Thread: MINOR char ERROR

  1. #1
    Unregistered
    Guest

    MINOR char ERROR

    In this program, I'm accepting the values for the variables you see here.. Everything is fine until output of NAME. It just skips Name and ends the program. Am i missing something??

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    
    void PRINT(int INT, float FLOAT, double DOUBLE, char MI, char NAME[30])
        {cout<<"\n Please enter a value for int: "; cin>>INT;
         cout<<" Please enter a value for float: "; cin>>FLOAT;
         cout<<" Please enter a value for double: "; cin>>DOUBLE;
         cout<<" Please enter a value for mi: "; cin>>MI;
         cout<<" Please enter a value for Name: "; cin.getline(NAME,30);
    
         cout<<"\n\n INT: "<<INT;
         cout<<"\n FLOAT: "<<FLOAT;
         cout<<"\n DOUBLE: "<<DOUBLE;
         cout<<"\n MI: " <<MI;
         cout<<"\n NAME: "<<NAME[30]; }
    
    
    main()
    {   int INT; float FLOAT; double DOUBLE; char MI, NAME[30];
    
        PRINT(INT, FLOAT, DOUBLE, MI, NAME);
    
          system("PAUSE");
          return 0;
    }
    Thanks for checkin' it out

  2. #2
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    when you cout your name, you actually couting only index 30 of the array. it should be like this:

    Code:
    cout<<"\n NAME: "<<NAME;
    our problem is when you enter an int before a string it doesnt clear the buffer so implement a small ClearBuffer() function like this:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    void ClearBuffer()
    {
         char Dummy[30];
         getline (cin, Dummy);
    }
    
    void PRINT(int INT, float FLOAT, double DOUBLE, char MI, char NAME[30])
        {cout<<"\n Please enter a value for int: "; cin>>INT;
         cout<<" Please enter a value for float: "; cin>>FLOAT;
         cout<<" Please enter a value for double: "; cin>>DOUBLE;
         cout<<" Please enter a value for mi: "; cin>>MI;
         ClearBuffer();
         cout<<" Please enter a value for Name: "; cin.getline(NAME,30);
    
         cout<<"\n\n INT: "<<INT;
         cout<<"\n FLOAT: "<<FLOAT;
         cout<<"\n DOUBLE: "<<DOUBLE;
         cout<<"\n MI: " <<MI;
         cout<<"\n NAME: "<<NAME[30]; }
    
    
    main()
    {   int INT; float FLOAT; double DOUBLE; char MI, NAME[30];
    
        PRINT(INT, FLOAT, DOUBLE, MI, NAME);
    
          system("PAUSE");
          return 0;
    }
    Last edited by xlnk; 04-17-2002 at 06:39 PM.
    the best things in life are simple.

  3. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    I tried that..it's still not working
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  4. #4
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    sorry i made some changes to it, try it again, instead of "string Dummy" try "char Dummy[30]"
    the best things in life are simple.

  5. #5
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    ...Still Unsuccessful..is this the only way of doing it? i also tried #define CB cin.ignore(cin.rdbuf()->in_avail())

    //
    ....
    ....
    ...
    cin>>MI; CB;
    ...
    ....
    //

    and it still doesn't work.
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    cout << flush;

  7. #7
    Unregistered
    Guest
    instead of clearBuffer() or #define CB (whatever) try just putting this in:


    cout<<" Please enter a value for mi: "; cin>>MI;
    cin.ignore();
    cout<<" Please enter a value for Name: "; cin.getline(NAME,30);


    assuming user only pushed enter key once after inputting data fro MI, then should work without stumbling over creating another function or writing a macro.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM