Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dynamic Array question

Started by graiglogan Apr 5, 2011 at 3:35 AM 2 replies 1k views
Original Post
graiglogan
graiglogan
Hello All, I am working through the C++ Primer Plus 5th Edition and I am having a slight problem with the following question:

I need a structure that takes the following information:
Pizza Company name
Diameter of the pizza
Weight of the pizza

The user needs to be able to enter the information and it has to be able to take more than one word answers for the pizza company name.

The line that i think is having the problem is the following:

cin.getline(ps.pizzaCompany, 20);

i had

cin << ps.pizzaCompany and it works with one word names but bugs out with two.

Now the problem that I am running into is that on the second pass through the loop it is printing the company name question and the diameter question without waiting for a response and I am not sure why.

I have the full code pasted below to help me find my problem...also let me know if anything should be done a different way or if it looks good the way it is.

Thanks,

Graig


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

struct PizzaAnalysis
{
char pizzaCompany[20];
int diameter;
int weight;
};


int main()
{
using namespace std;

PizzaAnalysis * ps = new PizzaAnalysis[3];
int i = 0;
while (i < 3)
{
cout << "what is the name of the pizza place? : " << endl ;
cin.getline(ps.pizzaCompany, 20);
cout << "What is the diameter of the pizza? : " << endl;
cin >> ps.diameter;
cout << "what is the weight of the pizza? : " << endl;
cin >> ps.weight;
i++;
}

i = 0;
while (i < 3)
{
cout << ps.pizzaCompany << endl;
cout << ps.diameter << endl;
cout << ps.weight << endl;
i++;
}

delete [] ps;
cin.get();
cin.get();
cin.get();
return 0;
}


SiCrane
SiCrane
>> and getline() work slightly differently and so when using the two on the same stream you can get weird behavior like you describe. If you use >> to read in an integer it removes all the characters that look like number digits and leaves whatever is left in the stream. getline() removes everything up to the next newline from the stream. So let's say your stream contains the contents "56\nfred\n". If you use >> to read in a integer it'll read the 56 and leave "\nfred\n" from the stream. If you then call getline() it looks for the first newline which is the first "\n" so it looks like it reads in nothing.

Rather than mix the two on the same stream I suggest using getline() with strings for every read. If you want to parse a particular line for an integer dump the string in a stringstream and read from the stringstream with >>.
graiglogan
graiglogan
OK i did that and it works, although I am not sure if this is the way the book was looking to do it or not. I am not familiar with stringstream yet but since these were basic excercises and we are not doing anything with the data then having them as strings would work. The books seems to jump from using char to string and using the different methods for each. I like the string way better but I was trying to see if I could get both ways to work. Not sure I fully understand the benefits and differences of dynamic/static arrays and how they are used but hopefully that will come soon enough.

Thanks for the help, I am sure that I will have more questions soon enough!
Trienco
Trienco
Since you're already using streams, you could even get fancy and use the global getline function in std (since for whatever reason the member version doesn't take strings).

string name;
getline(cin, name);
f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.