GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Sep-2003, 19:36
elavalos elavalos is offline
New Member
 
Join Date: Sep 2003
Posts: 2
elavalos is an unknown quantity at this point
Question

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:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdlib.h>

main() {
   
char n[5][50];
short S[5],E[5],EC[5],i,cuentaF=0,cuentaH=0;
for(i=0;i<5;i++){
    cout<<"Cual es tu nombre? ";
    cin.getline(n[i],50); 
    do{
        cout<<"Sexo:  1.- Femenino  2.- Masculino ";
        cin>>S[i];
    }while(S[i]<1 || S[i]>2);
    do{
        cout<<"Cual es su Edad? ";
        cin>>E[i];
    }while(E[i]<1 || E[i]>110);
    do{
        if(S[i]==2){
                cout<<"Estado Civil:  1.- Soltero  2.- Casado  3.- Diviorciado  4.- Viudo ";
        }else
                cout<<"Estado Civil:  1.- Soltera  2.- Casada  3.- Diviorciada  4.- Viuda ";
        cin>>EC[i];
    }while(EC[i]<1 || EC[i]>4);
    if((S[i]==1 && EC[i]==1) && (E[i]>=18 && E[i]<=21))
        cuentaF++;
    if((S[i]==2&&EC[i]==1)&&(E[i]>=18&&E[i]<=21))
        cuentaH++;
  
}


cout<<"Nombre"<<setw(30)<<"Edad"<<setw(10)<<"Sexo"<<setw(20)<<"Estado Civil"<<endl;
cout<<"--------------------------------------------------------------------"<<endl;
cout<<n[i][50]<<setw(30)<<E[i]<<setw(10)<<S[i]<<setw(20)<<EC[i]<<endl;
cout<<endl<<"Total de Mujeres solteras entre 18 y 21 años de edad: "<<cuentaF<<endl;
cout<<"Total de Hombres solteros entre 18 y 21 años de edad: "<<cuentaH<<endl;

system("PAUSE");
return 0;
}

I really would appreciate any help, thankyou
  #2  
Old 23-Sep-2003, 03:35
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdlib.h>

main() {

char n[5][50];
short S[5],E[5],EC[5],i,cuentaF=0,cuentaH=0;

for(i=0;i<5;i++){
  cout<<"Cual es tu nombre? ";
  cin.getline(n[i],50); 

  do{
    cout<<"Sexo: 1.- Femenino 2.- Masculino ";
    cin>>S[i];
  }while(S[i]<1 || S[i]>2);

  do{
    cout<<"Cual es su Edad? ";
    cin>>E[i];
  }while(E[i]<1 || E[i]>110);

  do{
    if(S[i]==2){
      cout<<"Estado Civil: 1.- Soltero 2.- Casado 3.- Diviorciado 4.-Viudo ";
    }else
      cout<<"Estado Civil: 1.- Soltera 2.- Casada 3.- Diviorciada 4.- Viuda ";
    cin>>EC[i];
  }while(EC[i]<1 || EC[i]>4);

  if((S[i]==1 && EC[i]==1) && (E[i]>=18 && E[i]<=21))
  cuentaF++;

  if((S[i]==2&&EC[i]==1)&&(E[i]>=18&&E[i]<=21))
  cuentaH++;


  cin.ignore();
}


cout<<"Nombre"<<setw(30)<<"Edad"<<setw(10)<<"Sexo"<<setw(20)<<"Estado Civil"<<endl;
cout<<"--------------------------------------------------------------------"<<endl;
cout<<n[i][50]<<setw(30)<<E[i]<<setw(10)<<S[i]<<setw(20)<<EC[i]<<endl;
cout<<endl<<"Total de Mujeres solteras entre 18 y 21 años de edad: "<<cuentaF<<endl;
cout<<"Total de Hombres solteros entre 18 y 21 años de edad: "<<cuentaH<<endl;

system("PAUSE");
return 0;
}

Hope this helps!
GF
  #3  
Old 23-Sep-2003, 16:40
elavalos elavalos is offline
New Member
 
Join Date: Sep 2003
Posts: 2
elavalos is an unknown quantity at this point
Smile

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... I'm using the same code as before with the additional cin.ignore(); I've modified the table code to this:

CPP / C++ / C Code:
cout<<"Nombre"<<setw(30)<<"Edad"<<setw(10)<<"Sexo"<<setw(20)<<"Estado Civil"<<endl;
cout<<"--------------------------------------------------------------------"<<endl;
for(i=0;i<5;i++){
    cout<<n[i][50]<<setw(30)<<E[i]<<setw(10)<<S[i]<<setw(20)<<EC[i]<<endl;
    }
    cout<<endl<<endl<<"Total de Mujeres solteras entre 18 y 21 años de edad: "<<cuentaF<<endl;
    cout<<"Total de Hombres solteros entre 18 y 21 años de edad: "<<cuentaH<<endl;

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 GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 02:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.