Home > Back-end >  Together to create a dynamic array structure and use a for loop input value assignment and the for o
Together to create a dynamic array structure and use a for loop input value assignment and the for o

Time:01-13

 # include 
//# include
# include
using namespace std;
//const int Max=10;
Struct PizzaData {
A string brand;
Float diameter;
Float weight;
};
Int main () {
PizzaData * p=new PizzaData [3].
for (int i=0; i<3; I++) {
Cout & lt; <"The brand of The pizza company:";
Getline (cin (p + I) - & gt; Brand);
//cin. The get ();
Cout & lt; <"The diameter is:";
Cin & gt;> P [I] diameter;
Cout & lt; <"The weight of The pizza is:";
Cin & gt;> (p + I) - & gt; weight;
}
For (int j=0; J<3; J++) {
Cout & lt; <"The brand of The pizza company is:" & lt;

Cout & lt; <"And it is" & lt;

}
The delete [] p;
P=NULL;
system("pause");
return 0;
}




Can only succeed to the structure of the array first member assignment, later after I input the second group of brand elements don't give me the input, and the error display

CodePudding user response:

do you want this effect??????? I ran with vscode seem to have no problem, attached code, changed some, to test the overall hasn't changed, you look at?
 struct PizzaData 
{
A string brand;
Float diameter;
Float weight;
};
PizzaData * p=new PizzaData [3].
for (int i=0; I & lt; 3; I++)
{
Cout & lt; <"The brand of The pizza company:";
Cin & gt;> P [I] brand;
//getline (cin (p + I) - & gt; Brand);
//cin. The get ();
Cout & lt; <"The diameter is:";
Cin & gt;> P [I] diameter;
Cout & lt; <"The weight of The pizza is:";
Cin & gt;> P [I]. Weight;
}
For (int j=0; J & lt; 3; J++)
{
Cout & lt; <"The brand of The pizza company is:" & lt;

Cout & lt; <"And it is" & lt;

}
The delete [] p;
P=NULL;

CodePudding user response:

I run on vs2015, but always in the for loop assignment of the second object, an input enter brand value is automatically at the back of the object assignment for me

CodePudding user response:

Find the problem, it is about getline (cin, string) the use of the problem, I try to use the simple cin input stream, and then to be able to complete the output,

CodePudding user response:

But such input only on a single word input, if there is a blank separated string input, will appear blank (Spaces, line breaks) stranded in the input stream, and the execution of the back of the input statement output to the back of the element object, in simple terms, is unable to process lines of input, I was using getline (big one) is in order to be able to input this string, simple to use cin can't achieve the result of further,
  • Related