Struct problems?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 6
Reputation: bjaanes is an unknown quantity at this point 
Solved Threads: 0
bjaanes bjaanes is offline Offline
Newbie Poster

Struct problems?

 
0
  #1
Jul 2nd, 2008
Hi =)

I am very new to c++ and have started messing around with some lines of code, but I am totaly stuck on this one:

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. int antnames;
  10. cout << "How many names do you want to put in the base? \n";
  11. cin >> antnames;
  12.  
  13. struct names
  14. {
  15. string namee;
  16. int age;
  17. } namess[antnames];
  18.  
  19. int n;
  20. string mystr;
  21. for (n = 0; n<antnames; n++)
  22. {
  23. cout << "Enter name: ";
  24. getline(cin,namess[n].namee);
  25. cout << "Enter age for this person: ";
  26. getline(cin,mystr);
  27. stringstream(mystr) >> namess[n].age;
  28. }
  29.  
  30. int nn;
  31. for (nn = 0; nn<antnames; nn++)
  32. {
  33. cout << "Name: " << namess[nn].namee << "\n";
  34. cout << "Age: " << namess[nn].age << "\n";
  35. }
  36. cin.get();
  37.  
  38. }

When the codes execute and hits this point:
for (n = 0; n<antnames; n++)
{
cout << "Enter name: ";
getline(cin,namess[n].namee);
cout << "Enter age for this person: ";
getline(cin,mystr);
stringstream(mystr) >> namess[n].age;
}


In the FIRST round in the loop it does not let me enter the first name.
In the second and so on it works just fine.

There was no problem when i used #DEFINE antnames instead of user input.
But this is not the desired effect for me =)

Can anyone help me out here?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Struct problems?

 
0
  #2
Jul 2nd, 2008
The size of the array has to be known at compile time. If it has to be decided at run-time please use dynamic arrays. When you used #Define the size was known at compile time hence it worked.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: bjaanes is an unknown quantity at this point 
Solved Threads: 0
bjaanes bjaanes is offline Offline
Newbie Poster

Re: Struct problems?

 
0
  #3
Jul 2nd, 2008
Right, right.
Thanks!

But how do that for an array in the structure?

struct names
{
string namee;
int age;
} namess[antnames];

How to use dynamic memory on the namess array?

I have tried around for a bit, but seems to be rather stuck again.
Last edited by bjaanes; Jul 2nd, 2008 at 6:41 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 41
Reputation: RenjithVR is an unknown quantity at this point 
Solved Threads: 7
RenjithVR RenjithVR is offline Offline
Light Poster

Re: Struct problems?

 
0
  #4
Jul 2nd, 2008
like this,
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. int antnames;
  10. cout << "How many names do you want to put in the base : ";
  11. cin >> antnames;
  12. struct names
  13. {
  14. string namee;
  15. int age;
  16. };
  17. names *details = new names[antnames];
  18. string mystr;
  19. cin.get();
  20. for (int n = 0; n<antnames; n++)
  21. {
  22. cout << "\nEnter name: ";
  23. getline(cin, details[n].namee);
  24. cout << "\nEnter age for this person: ";
  25. getline(cin,mystr);
  26. stringstream(mystr) >> details[n].age;
  27. }
  28. int nn;
  29. for (nn = 0; nn<antnames; nn++)
  30. {
  31. cout << "Name: " << details[nn].namee << "\n";
  32. cout << "Age: " << details[nn].age << "\n";
  33. }
  34. }
Last edited by RenjithVR; Jul 2nd, 2008 at 6:52 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: bjaanes is an unknown quantity at this point 
Solved Threads: 0
bjaanes bjaanes is offline Offline
Newbie Poster

Re: Struct problems?

 
0
  #5
Jul 2nd, 2008
I finally got i working =)

Very good! Thank you.

But why does it seem like cin.get() was one of the reasons it jumped over the first name entry?
(need to learn this stuff to ^^)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Struct problems?

 
0
  #6
Jul 2nd, 2008
That's because you're mixing cin << with cin.get();
cin << ... leaves a "\n" (end of line) character on the stream and when you then call cin.get(), that character is still on the stream, so you aren't able to type anything. (cin.get only takes 1 char)

To avoid this problem, use:
  1. #include <limits.h>
  2.  
  3. [...code...]
  4. cin.ignore ( INT_MAX, '\n' );
  5. cin.get();
Last edited by niek_e; Jul 2nd, 2008 at 7:15 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC