读取字符列表并存储在数组中
Reading a list of characters and storing in an array
我对以下代码有问题,我不知道为什么没有打印循环外。有了这个代码,我希望程序忽略用户输入的任何空格,在输入空格后,之前输入的数字将存储在数组位置。像这样,我希望6和78存储在2个阵列位置,而不是将它们单独存储为6 7 8。
这是我的代码:
while ((in=getchar()) != '0')
{
if (in == ' ')
{
printf("spacen ");
continue;
}
else
{
printf("assigningn ");
input[i]=in;
}
i++;
}
printf("Out of Loop");
当输入5 6 78时,我的输出是:分配空间分配空间分配分配分配
对于这个输出,我怀疑78是否被存储在一个存储位置中。
我真的很感谢你的帮助,谢谢
C++:
std::vector<int> v;
std::string s;
int i;
std::getline( std::cin, s); // read full line with whitespaces
std::istringstream iss( s); // prepare to process the line
while( iss >> i) v.push_back( i); // read into i and push into vector if
// operator>> was successful
C:
int array[ 10];
int i = 0, retval;
while( i < 10 && ( retval = scanf( "%d", &array[ i++])) == 1) ;
if( i == 10) {
// array full
}
if( retval == 0) {
// read value not an integer. matching failure
}
if( retval == EOF) {
// end of file reached or a read error occurred
}
您正在逐个字符进行决定。因此,您将只存储单个数字或忽略这些数字。
你可以像这样存储整个数字(扩展你的代码):
bool currentNumberStarted = false;
int currentNumber = 0;
int idx = 0;
while ((in=getchar()) != '0')// you probably want ' ' instead of '0'
{
if (in == ' ')
{
if (currentNumberStarted)
{
input[idx]=currentNumber;
idx++;
currentNumberStarted = false;
}
printf("spacen ");
continue;
}
else
{
printf("assigningn ");
currentNumberStarted = true;
currentNumber *= 10;
currentNumber += in;
}
}
printf("Out of Loop");
首先,我非常怀疑while循环是否会结束,即使您对' '
进行了此操作,因为您使用的是char
变量来存储输入。不是字符串,只有字符串在末尾使用' '
,我们如何从键盘输入"\0"。。???。即使你想把它保持为'0'
,你也必须输入0作为最后一个数字来结束循环(我认为你不想这样做。)
因此,解决方案是:-
输入数字后,您将按ENTER键,这将生成一个换行符"\n",因此您必须检查换行符('n'
),并且当您使用getchar()函数时,它将在输入结束时返回EOF(-1),因此检查它也很重要。因此,您必须在while循环中同时检查'\n'和EOF。最后,您还应该检查存储数字的数组索引号(应该小于1)。
我在评论中努力让你理解这个节目。
int main()
{
int i=0;
int input[10]={0}; //here only 10 integers can be entered(hence i should be i<10)
int in; //To store input character
int num=0; //To store number which is converted from character.
int new=1; //To check if new number is started 0=false 1=True.
int count=0;//This is just to know how many numbers entered,also used to print numbers at end.
while ((in=getchar()) != 'n' && (in!=EOF) && i<10)//should check for both 'n' and EOF and array index also
{
if (in == ' ')
{
printf("spacen ");
if(new==0) //if new Number is not started yet.
{
new=1; //Start of a New number.(a number entered after space)
i++; //As new number is started it should be stored in new array index.
}
continue; //if space is entered just go to begining
}
else
{
printf("assigningn ");
num=in-48; //converts a character to number (ex:- converts '3' to 3)
input[i]=(input[i]*10)+num; //storing the number..This is important do a paper work to understand this step.
new=0; //still in same number(we are still processing same number)
}
}
printf("Out of Loop n");
count=i+1; //This gives correct count of numbers entered
for(i=0;i<count;i++) //to print numbers.
printf("%d ",input[i]);
return 0;
}
输出:-
E:>example.exe
78 2 65 998 1
分配
分配
空间
分配
空间
空间
分配
环路外
78 2 65 998 1
相关文章:
- 多维数组存储三种不同的数据类型?
- C++将原始字符数组存储在元组中
- 将十六进制字符数组存储在字节数组中,而无需更改为 ASCII 或其他任何东西
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- 如何将JSON数组存储在Arduino程序中
- 如何在AVR(例如:ATMega)或Arduino微控制器上制作2D PROGMEM阵列的1D PROGMEM数组(存储
- 使用 char 数组存储数据
- 使用平面数组存储的元组容器
- 数组 存储并要求用户搜索它
- 将值从一个 char 数组存储到另一个 char 数组
- 如何使字符串数组存储在向量中
- 将构造的静态数组存储在多个文件中的内存中
- 将整数数组存储在堆上并通过指针访问它们
- c++声明一个数据类型的字节数组,该数组存储在字符串中
- 使用指针数组存储阶乘计算的所有中间结果
- 使用异构数组存储不同的子类
- 读取文件内容,存储在数组中,向数组添加更多内容,然后将新数组存储在文件C++中
- 尝试将复杂数组存储到类 c++ 中
- 在字节数组存储中嵌入 int/string