关于一个共用体字符串输入的问题
2014-01-07 17:19
253 查看
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
union computerInfo
{
char band[20];
float price;
};
void main(void)
{
union computerInfo comp1 = {"Asus X80"};
//union computerInfo comp1 ;
int type = 0;
puts("Please input the type of the computers:");
scanf("%d",&type);
switch(type)
{
case 0:
puts("please input the band of the computer");
scanf("%s",&comp1.band)(1);
//scanf("%s",comp1.band)(2);
//gets(&comp1.band)(3);
//gets(comp1.band)(4);
问题出现在这里: 在这里(1)和(2)两个表达式都是可以执行的,为什么呢?按道理来说,(1)是不对;(2)是正确的,但是不能输入空格隔开的单词;(3)是错的,编译的时候就会通知有错误;(4)编译可以通过,但是程序执行的时候,不需要输入,直接就略过了输入品牌的这一步为什么?
printf("the type of the computer is: %s\n",comp1.band);
break;
case 1:
puts("please input the price of the computer");
scanf("%f",&comp1.price);
printf("The price of the computer is %f\n",comp1.price);
break;
default:
puts("The input type is wrong!");
break;
}
}
问题就在上面,求大侠解答。
# include <conio.h>
# include <stdlib.h>
union computerInfo
{
char band[20];
float price;
};
void main(void)
{
union computerInfo comp1 = {"Asus X80"};
//union computerInfo comp1 ;
int type = 0;
puts("Please input the type of the computers:");
scanf("%d",&type);
switch(type)
{
case 0:
puts("please input the band of the computer");
scanf("%s",&comp1.band)(1);
//scanf("%s",comp1.band)(2);
//gets(&comp1.band)(3);
//gets(comp1.band)(4);
问题出现在这里: 在这里(1)和(2)两个表达式都是可以执行的,为什么呢?按道理来说,(1)是不对;(2)是正确的,但是不能输入空格隔开的单词;(3)是错的,编译的时候就会通知有错误;(4)编译可以通过,但是程序执行的时候,不需要输入,直接就略过了输入品牌的这一步为什么?
printf("the type of the computer is: %s\n",comp1.band);
break;
case 1:
puts("please input the price of the computer");
scanf("%f",&comp1.price);
printf("The price of the computer is %f\n",comp1.price);
break;
default:
puts("The input type is wrong!");
break;
}
}
问题就在上面,求大侠解答。
相关文章推荐
- 一个关于 字符与字符串 的 小问题
- 用java写关于删除一个字符串的字符以及删除一个字符串与另一个字符串中相同字符的问题
- 关于c++中字符串的输入问题
- 关于字符串指针初始化std::string的一个小问题
- 关于输入一个ID后,该ID的2个类别展示的问题
- 关于《程序员编程宝典》中编写一个函数,作用是把一个char组成的字符串循环右移n位的问题
- cocos2dx中使用的tolua关于字符串处理的一个问题
- 关于数组与字符串输入的问题-C++版 推荐
- 关于“c语言从键盘输入字符串到指针数组”的问题
- 关于字符串输入的问题
- 字符串输入连接比较还有关于cin的问题
- 关于输入字符串的问题
- 问题:求两个字符串的最长公共子串。 要求:输入两个字符串,输出它们的最长公共子串,包括长度。 设计: 设计一个类 String,包括一个 len(字符串长度)和字符串指针 s。另有如下成员函数: ·
- 一个关于c++字符串处理和delete[]与delete差别的问题
- 关于字符串截取的一个小问题
- 一个关于C语言输入的问题
- VC中一个关于宏的使用问题,字符串之间转换,宽字符与普通字符
- 关于连接字符串中IMEX参数的一个问题
- 关于c里面 用 %[] 输入字符串的问题 【摘自旧博】
- 统计设计的问题和输入一个字符串的问题