943,673 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 663
  • C++ RSS
Jul 2nd, 2008
0

Struct problems?

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bjaanes is offline Offline
6 posts
since Jul 2008
Jul 2nd, 2008
0

Re: Struct problems?

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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jul 2nd, 2008
0

Re: Struct problems?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bjaanes is offline Offline
6 posts
since Jul 2008
Jul 2nd, 2008
0

Re: Struct problems?

like this,
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 12
Solved Threads: 7
Light Poster
RenjithVR is offline Offline
41 posts
since Mar 2008
Jul 2nd, 2008
0

Re: Struct problems?

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 ^^)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bjaanes is offline Offline
6 posts
since Jul 2008
Jul 2nd, 2008
0

Re: Struct problems?

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:
cpp Syntax (Toggle Plain Text)
  1. #include <limits.h>
  2.  
  3. [...code...]
  4. cin.ignore ( INT_MAX, '\n' );
  5. cin.get();
Last edited by Nick Evan; Jul 2nd, 2008 at 7:15 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: while loop dont seem to work
Next Thread in C++ Forum Timeline: Fstream Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC