Results 1 to 7 of 7

Thread: cin.getline ahhh!!!!!!!

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    in front of my desk
    Posts
    15

    cin.getline ahhh!!!!!!!

    ok i'm tryin to learn c++ and i'm using microsoft visual c++ (xmas present from my uncle) ok here's my problem, i have this function

    Code:
    void pause()
    {
    	char mybuffer [100];
    	cout << "Press Any key to continue";
    	cin.getline (mybuffer,100);
    
    }
    but everytime i call it it just flies right over the cin.getline unless i call it before the last like 5 lines of main

    Code:
    int main()
    {
    	char choicetmp[5];
    	int choice;
    //	char tempvar [256];
    	cout << "1.) Add 2 Numbers" << endl;
    	cout << "2.) Subtract 2 Numbers" << endl;
    	cout << "3.) Divide 2 Numbers" << endl;
    	cout << "4.) Multiply 2 Numbers" << endl;
    	cout << endl;
    	cout << "Enter the number you want: ";
    	cin.getline(choicetmp,5);
    	choice = atoi(choicetmp);
    	switch(choice)
    	{
    		case 1: add();
    	}
    	pause();	
    	return 0;
    }
    i have no idea what it is but it's driving me nuts!! any help would be appreciated!
    "Run Run Run as fast as you can.... you can't catch me... i'm the possessed mental case man"

  2. #2

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    in front of my desk
    Posts
    15
    OH YEA GO ME!!@#!@%
    GO FARKIN ME!@$!@%

    figured it out myself... apparently cin >> and cin.getline do not cooperate so just used cin.getline and atoi to make the char int!
    "Run Run Run as fast as you can.... you can't catch me... i'm the possessed mental case man"

  3. #3
    Member TheGuru's Avatar
    Join Date
    Dec 2001
    Posts
    57
    Ya usually sya something like
    cin >> x;
    for int's

    and cin.getline
    for strings

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    in front of my desk
    Posts
    15
    yea but when you use cin >> with cin.getline it goes all screwy and cin.getline reads what was put to the cin >> so you just gotta use atoi to like convert it from chars to int... dunno i think i'm just like making it work tho
    "Run Run Run as fast as you can.... you can't catch me... i'm the possessed mental case man"

  5. #5
    Member TheGuru's Avatar
    Join Date
    Dec 2001
    Posts
    57
    You don't want to use cin >> and getline on the same line hehe.

    Here is a example:

    #include <iostream.h>

    int main()
    {
    int x;

    cout << "Enter a number:";
    cin >> x;

    cout << endl;
    cout << "The Number You Entered Was:" << x << endl;

    return 0;
    }

    That's the simplest way to collect numbers..

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2001
    Location
    in front of my desk
    Posts
    15
    yea but what i was trying to do was add a pause thing

    void pause()
    {
    char crap [1];
    cout << "Press any key to continue"l
    cin.getline (crap,1);
    }

    but THEN like 10 mins later i learn about about system("pause") and all so now i'm like yippie!!
    "Run Run Run as fast as you can.... you can't catch me... i'm the possessed mental case man"

  7. #7
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    If you want the program to halt, try putting in cin.get(); once or twice in your code and it will need a key-press to proceed......
    [p r a e t o r i a n]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width