cin problem

This is a discussion on cin problem within the C++ Programming forums, part of the General Programming Boards category; for some reason it skips the input line for first name. i have tried using cin.getline(fname, 20, '\n') and cin.ignore() ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    33

    cin problem

    for some reason it skips the input line for first name. i have tried using cin.getline(fname, 20, '\n') and cin.ignore() but niether fix the problem. thanks for any help

    Code:
    int student::getID() {
    	
    	cout<<"STUDENT ID: ";
    	cin>>id;
    	
    	return 0;
    }
    
    char student::getNAME() {
    	
    	cout<<"FIRST NAME: ";
    	cin>>fname;
    	cout<<endl<<"MIDDLE NAME: ";
    	cin>>mname;
    	cout<<endl<<"LAST NAME: ";
    	cin>>lname;
    
    	strupr(fname);
    	strupr(mname);
    	strupr(lname);
    		
    	return 0;
    
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    how/where did you use them?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    do you call getID first? If so, then you have to make sure the stream is empty when you use cin >> fname. When the user presses "enter" after being prompted "student ID: ", a newline character is put into the stream and never taken out.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    33
    Here is all of my code. i some what got it to work now. the output is at the bottom.

    Code:
    #include "stdafx.h"
    #include "iostream.h"
    #include "string.h"
    #include "stdlib.h"
    #include "fstream.h"
    
    void title();
    
    class student {
    
    public:
    
    	int getID();
    	char getNAME();
    	char getSS();
    	char getADDRESS();
    	char getCOURSES();
    	char getTERM();
    	char getMAJOR();
    	char getGRADE();
    	char getADVISOR();
    	void showID();
    	void showNAME();
    	void showSS();
    	void showADDRESS();
    	void showCOURSES();
    	void showTERM();
    	void showMAJOR();
    	void showGRADE();
    	void showADVISOR();
    
    private:
    
    	long id;
    	int zip;
    	char fname[20], mname[20], lname[20], ss[15], street[80], town[20], state[2];
    	char courses[10], pname[20], days[5], stime[5], etime[5], term[15], major[80];
    	char grade[2], advisor[20];
    };
    
    int main()
    {
    	
    	student newSTUDENT;
    
    	system("color 0A");
    	title();
    	newSTUDENT.getID();
    	newSTUDENT.getNAME();
    	newSTUDENT.getSS();
    	newSTUDENT.getADDRESS();
    	newSTUDENT.getCOURSES();
    	newSTUDENT.getTERM();
    	newSTUDENT.getMAJOR();
    	newSTUDENT.getGRADE();
    	newSTUDENT.getADVISOR();
    	system("cls");
    	title();
    	newSTUDENT.showID();
    	newSTUDENT.showNAME();
    	newSTUDENT.showSS();
    	newSTUDENT.showADDRESS();
    	newSTUDENT.showCOURSES();
    	newSTUDENT.showTERM();
    	newSTUDENT.showMAJOR();
    	newSTUDENT.showGRADE();
    	newSTUDENT.showADVISOR();
    
    	return 0;
    }
    
    void title() {
    
    	cout<<"*******************************************************************************"<<endl<<endl<<endl;
    	cout<<"                 Welcome To My First Database Program!                           "<<endl<<endl;
    	cout<<"*******************************************************************************"<<endl<<endl;
    
    }	
    
    int student::getID() {
    	
    	cout<<"STUDENT ID: ";
    	cin>>id;
    	cin.ignore(7, '\n');
    	
    	return 0;
    }
    
    char student::getNAME() {
    	
    	cout<<endl<<"FIRST NAME: ";
    	cin>>fname;
    	cout<<endl<<"MIDDLE NAME: ";
    	cin>>mname;
    	cout<<endl<<"LAST NAME: ";
    	cin>>lname;
    
    	strupr(fname);
    	strupr(mname);
    	strupr(lname);
    		
    	return 0;
    
    }
    
    char student::getSS() {
    
    	cout<<endl<<"SOCIAL SECURITY: ";
    	cin>>ss;
    	cin.ignore(15, '\n');
    		
    	return 0;
    
    }
    
    char student::getADDRESS() {
    
    	cout<<endl<<"STREET ADDRESS: ";
    	cin.getline(street, 80, '\n');
    	cin.ignore(80, '\n');
    	cout<<endl<<"TOWN: ";
    	cin>>town;
    	cout<<endl<<"STATE: ";
    	cin>>state;
    	cout<<endl<<"ZIP CODE: ";
    	cin>>zip;
    	cin.ignore(7, '\n');
    
    	strupr(street);
    	strupr(town);
    	strupr(state);
    		
    	return 0;
    
    }
    
    char student::getCOURSES() {
    
    	cout<<endl<<"COURSES: ";
    	cin.getline(courses, 10, '\n');
    
    	strupr(courses);
    		
    	return 0;
    
    }
    
    char student::getTERM() {
    
    	cout<<endl<<"TERM: ";
    	cin.getline(term, 15, '\n');
    	cin.ignore(15, '\n');
    
    	strupr(term);
    		
    	return 0;
    
    }
    
    char student::getMAJOR() {
    
    	cout<<endl<<"MAJOR: ";
    	cin.getline(major, 80, '\n');
    
    	strupr(major);
    		
    	return 0;
    
    }
    
    char student::getGRADE() {
    
    	cout<<endl<<"CLASS: ";
    	cin>>grade;
    
    	strupr(grade);
    		
    	return 0;
    
    }
    
    char student::getADVISOR() {
    
    	cout<<endl<<"ADVISOR: ";
    	cin.getline(advisor, 20, '\n');
    	cin.ignore(20, '\n');
    	
    	strupr(advisor);
    		
    	return 0;
    
    }
    
    void student::showID() {
    
    	cout<<endl<<"ID: "<<id<<endl;
    
    }
    
    void student::showNAME() {
    
    	cout<<endl<<"NAME: "<<fname<<" "<<mname<<" "<<lname<<endl;
    
    }
    
    void student::showSS() {
    
    	cout<<endl<<"SOCIAL SECURITY: "<<ss<<endl;
    
    }
    void student::showADDRESS() {
    
    	cout<<endl<<"ADDRESS: "<<endl<<street<<endl<<town<<", "<<state<<" "<<zip<<endl;
    
    }
    
    void student::showCOURSES() {
    
    	cout<<endl<<"COURSES:"<<endl<<courses<<endl;
    
    }
    
    void student::showTERM() {
    
    	cout<<endl<<"TERM: "<<term<<endl;
    
    }
    
    void student::showMAJOR() {
    
    	cout<<endl<<"MAJOR: "<<major<<endl;
    
    }
    
    void student::showGRADE() {
    
    	cout<<endl<<"CLASS: "<<grade<<endl;
    
    }
    
    void student::showADVISOR() {
    	
    	cout<<endl<<"ADVISOR: "<<advisor<<endl;
    
    }
    I know its kinda long but i am just being bored tonight ...ok now here is the out put after it clears the screen:

    ************************************************** ***************************


    Welcome To My First Database Program!


    ************************************************** ***************************


    ID: 136499

    NAME: WANTED STUFF ALLDAY

    SOCIAL SECURITY: 466716873

    ADDRESS:
    1234 MAIN STREET
    AUSTIN, TXCPTR 234 78602

    COURSES:
    CPTR 234

    TERM: SPRING 2004

    MAJOR: COMPUTER SCIENCE

    CLASS: FR

    ADVISOR:
    Press any key to continue

    My problems are:
    i have to hit enter twice for the street address and for term
    if you look after the "AUSTIN, TX" it has the course for somereason
    last but not least the ADVISOR: doesnt show what i entered
    Last edited by Wanted420; 03-13-2004 at 11:08 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    33
    O yeah the ID output is not what i inputted ... it gives me 756 i think everytime i enter 0136499 but if i just put 136499 i get 136499. so why cant i have the 0? is it to long of a number?

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by Wanted420
    O yeah the ID output is not what i inputted ... it gives me 756 i think everytime i enter 0136499 but if i just put 136499 i get 136499. so why cant i have the 0? is it to long of a number?
    I don't know if this is the problem you're asking but if you want to keep the zeros you have to store them in char array/string;
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    33
    well then i guess i will need to change it to an array/string

    thanks

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Your ignore statments are causing the problems

    The reason you are having skipping problems and also needing to hit enter twice is that your ignore statments are incorrect. when you have an ignore and nothing is in the buffer it waits until you hit enter. you should only need ignore statments on the long and int values which are the zip code and the id the rest are all chars so you shouldn't need an ignore for those.

    Also for the Id and zip code you need to validate the input. if you enter a letter instead of a number it will crash because cin will be in a fail state. Search the boards and check the faqs and tutorials for input with cin.

    [edit]
    Just an added tidbit. Since this is a student ID it would be better to keep the number as a char since you would need to preserve the leading 0's. otherwise you can use
    Code:
    cin>>dec;
    What this does is set the base value to decimal. If you want to set it back to oct or hex just repeat the above statment and replace with whichever base you need. It will still strip the leading 0 from the number if you want to padd the value to left or right justify then you can use
    Code:
    cout.width(x);
    cout.fill('0');
    cout<<dec;
    cout<<number;
    but only if you know the number has a fixed width.
    [/edit]
    Last edited by manofsteel972; 03-14-2004 at 03:43 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The 0 problem is due to number parsing. With the 0 in front, it tries to parse the number as octal. Since 9 is not an octal digit it only parses "1364", the decimal equivalent of which is 756.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  2. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 07:43 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with cin
    By ErionD in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 10:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21