Home > Back-end >  Why C loop input structure elements could go wrong?
Why C loop input structure elements could go wrong?

Time:09-20

Input: aa aa, left behind without input, and can't display properly, especially the cycle 3. What are the reasons?
I know that is the reason of cin, but I don't understand why is cin, I change cin getline, and will have new problems,


Code:
 
//structur. CPP -- -- a simple structure
#include
#include
Struct inflatable//structure declaration
{
char name[20];
Float volume;
double price;
};

Int main ()
{
using namespace std;
Inflatable guest=
{
"Glorious Gloria",//the name value
1.88,//volume value
29.99//price value
};//guest is a structure variable of type inflatable
//It 's an initialized to the indicated values
Inflatable Pal=
{
"Audacious Arthur,"

3.12,32.99
};//Pal is a second variable of type inflatable
//NOTE: some implementations require using
//static inflatable guest=

Cout & lt; <"Expand your guest list with" & lt; Cout & lt; <"And" & lt; //Pal. The name is the name member of the Pal variable
Cout & lt; <"You can have both for $";
Cout & lt; //cin. The get ();

//my structure array test
Inflatable guest_array [3].
for (int i=0; i <3; I++)
{
Cout & lt; <"Both Please input the name:" & lt; //cin. Getline (guest_array [I]. Name, 20).
Cin & gt;> Guest_array [I]. Name;

Cout & lt; <"Both Please input volume:" & lt; Cin & gt;> Guest_array [I]. Volume;

Cout & lt; <"Both Please input price:" & lt; Cin & gt;> Guest_array [I]. Price;

Cout & lt; <"Name:" & lt; Cout & lt; <"Volume:" & lt; Cout & lt; <"Price:" & lt; }


return 0;
}


CodePudding user response:

For each data input, and press enter

CodePudding user response:

reference 1/f, Simple, Soft reply:
for each data input, just press enter

Screenshot of the 3 don't understand what's wrong,

CodePudding user response:

You know the debugged

CodePudding user response:

reference 3 floor is Simple - Soft reply:
debug once you know the

Because debugging don't understand just post

CodePudding user response:

Don't consider this question, I figure out in another post "c + + how to ensure that the input is always Numbers rather than the character", loop input digital or character is my purpose,
https://bbs.csdn.net/topics/396698161

The final code:
//my_structur. CPP - create an array of structure, loop input structure element, 
//1 create structure;
//2 create an array of structure.
//3 input loop structure elements to ensure always input string or number.
//3.1 while (1) {}
#include
#include
Struct inflatable//structure declaration
{
char name[20];
Float volume;
double price;
};

Int main ()
{
using namespace std;
//my structure array test
Inflatable guest_array [3].
for (int i=0; i <3; I++)
{
Cout & lt; <"Both Please input the name:" & lt; //cin. Getline (guest_array [I]. Name, 20).
//cin> Noskipws & gt;> Guest_array [I]. Name;

Cin. Get (guest_array [I]. Name, 20).

Cout & lt; <"Both Please input volume:" & lt; while (! (cin & gt;> Guest_array [I]. Volume))
{
Cout & lt; <"Invalid input! Please enter the Numbers again: "& lt; Cin. The clear ();
Cin. Ignore (1024), '\ n');
}

//while (! The scanf (" % d ", & amp; Guest_array [I]. Volume) | | guest_array [I]. Volume & lt; 0)
//{
//printf (" invalid input! Please input again: \ n ");
//the scanf (" % * [^ \ n] ");
//the scanf (" % * c ");
//}

Cin. The get ();

Cout & lt; <"Both Please input price:" & lt; Cin & gt;> Guest_array [I]. Price;
Cin. The get ();

Cout & lt; <"Name" & lt; Cout & lt; <"Volume:" & lt; Cout & lt; <"Price:" & lt; }

return 0;
}
  • Related