Thread: cin.getline anamoly??

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    55

    cin.getline anamoly??

    I don't get it. so i have a program. the follow function is executed when I make a selection on the main menu of the program.

    Only when i write "cin.getline (ch2,21);"twice this function works right. It prompts you,then you write your string, and it prints the correct values.

    if i type "cin.getline (ch2,21);" once, it will just skip the line that asks you to type in a string.. what's the deal??



    Code:
    void StringType(void)
    {
    	char ch2[21];
    	char conti6;
    	int *strp;
    	int straddr;
    
    	cout << endl;
    	cout << endl;
    	cout << "Enter a string (20 char max):";
    	//cin >> ch2;
    	
    	cin.getline (ch2,21);
    	cin.getline (ch2,21);
    
    	strp = (int*)(&ch2);
    	straddr = int(strp);
    	
    	cout << endl;
    	cout << endl;
    	cout << "Address:0x";
    
    	straddr = int(strp);		//bit shifting for output formatting
    	straddr &= 0xFF000000;
    	straddr = straddr >> 24;
    	straddr &= 0x000000FF;
    	if(straddr < 16) cout << "0";
    	cout << hex << straddr << " ";
    
    	straddr = int(strp);
    	straddr &= 0x00FF0000;
    	straddr = straddr >> 16;
    	if(straddr < 16) cout << "0";
    	cout << hex << straddr << " ";
    
    	straddr = int(strp);
    	straddr &= 0x0000FF00;
    	straddr = straddr >> 8;
    	if(straddr < 16) cout << "0";
    	cout << hex << straddr << " ";
    
    	straddr = int(strp);
    	straddr &= 0x000000FF;
    	if(straddr < 16) cout << "0";
    	cout << hex << straddr << " ";   // bit shifting end.
    
    	cout << endl;
    	cout << "ASCII code:0x ";
    
    	for(int j = 0; j != 21; j++)
    	{
    		if(int(ch2[j] == 0))
    		{
    		cout << "00";
    		break;
    		}
    		cout << int(ch2[j]) << " ";
    
    	}
    
    
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    
    	while(1)
    	{
    		cout << "press 'c' and then enter to continue:";
    		cin >> conti6;
    		cout << endl;
    		if(conti6 == 'c' || conti6 == 'C') break;
    	}
    
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    	cout << endl;
    
    }

  2. #2
    Compile Errors = Schwa?!
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    15
    Could you post the code of the function that is calling it?

    I'm thinking the problem is with the function that calls this function. wherever you have:

    Code:
               int Input;
    
               cin>> Input;
               
    switch (Input) {
                      case 1:
                                 //code
                      case 2:
                                 //code
                      default:
                                //code
                      }
    
    //You need cin.ignore(); after cin>> Input;
    I'm pretty new to C++ so I could be wrong.

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    You get this when you mix cin>> and cin.getline();
    Try a board search. This same question comes atleast twice week.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    lol. very true. and i've asked one of those questions too.

    if you're using msvc++, get the free sp6 download from ms. and then apply the tiny change if it already hasn't been implemented in sp6.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    i've tried just using cin >> ... but it does work either.. anyway.. thanks i'll do a search

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    thanks, saw the answer in another post

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. cin.getline()
    By Ifurita. in forum C++ Programming
    Replies: 4
    Last Post: 05-29-2003, 01:14 PM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM