![]() |
|
#1
|
|||
|
|||
Help making a survey in C++Hi, I'm taking beginner courses in C++ and I have been looking for help with my program to no avail. I am making a simple survey in C++, it asks your name, sex, age, and marriage status in a cycle 5 times. After that it should print a table with name, sex, age, and marriage of the 5 people surveyed. My problem is that I haven't managed to make C++ remember 5 char's (the names of people), with their respective age, sex, etc. It will go through the for cycle one time asking all the questions, but on the second run it skips the name. I haven't gotten to making the data table yet since it won't take the 5 names yet, but at the end of the code you should see an idea of what it would be like. The problem is with the cin.getline(n[i],50). The rest of the questions work perfectly. Can anyone please help?? Here is the code:
CPP / C++ / C Code:
I really would appreciate any help, thankyou |
|
#2
|
|||
|
|||
|
Hey evalos, and welcome to the boards!
You've stumbled across a common problem. It's a technicality resulting from using cin and getline together. Getline accepts any input until it gets an \n - an end-of-line. It stores the input, and discards the \n cin << also accepts any input until an \n, but it leaves the \n in the buffer for the next function to recieve So when you run the loop the frist time, the buffer is empty. getline() recieves it's input, and takes the \n from the buffer. But then the cin gets it's input but leaves the \n - which getline() thinks is it's input. It's a silly technicality, but annoying! To fix it, you use the function ignore(), member of the iostream class. This function ignores the first character in the buffer. Placew this after the last cin in the loop. So your code will be: CPP / C++ / C Code:
Hope this helps! GF |
|
#3
|
|||
|
|||
|
Thanks Garth, that certainly did the trick. It will run and ask for the 5 names it is supposed too, each with their appropiate values. The only problem I see after compiling is that no matter what you input as a name, the table will only print the first letter of each, even though it is the correct letter for each one. Like so..
Nombre --------- F 21 R 21 G 19 T 21 " 20 I really appreciate the help, I really know what the program should do, and it looks like it does it, but something goes wrong there also... CPP / C++ / C Code:
I needed the additional "for" so it would cycle the five entries and display them, as it is supposed to show up after all the information is taken. Any ideas? Thank you again. |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| making pdf files downloadable | shahbaz | Web Design Forum | 3 | 10-Jul-2003 15:43 |
| Dot coms are making a comeback for businesses | jrobbio | Advertising & Affiliates Forum | 8 | 14-May-2003 09:54 |
| Suggestion of making some themed buttons | jrobbio | GIDTopsites™ | 3 | 26-Mar-2003 02:32 |
| making your image colours really bright | JdS | Graphics Forum | 0 | 29-Aug-2002 04:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The