gets() Problem

This is a discussion on gets() Problem within the C++ Programming forums, part of the General Programming Boards category; The read_integer function in the code below is weird. The first time the program runs through the loop it works ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    gets() Problem

    The read_integer function in the code below is weird. The first time the program runs through the loop it works just fine, taking in 2 integers and multiplying and adding them. However, if you choose to continue. It won't let you enter the first deque... It just prints out the "Please type a large integer:" and then prints it again immediately afterwards. It will allow you to enter a deque after that. I ran through it with the Debugger and it seems to be a problem with gets. But I could be wrong! Any suggestions?

    Code:
    #include <iostream>
    #include <cstdio>
    #include <deque>
    
    using namespace std;
    
    
    typedef deque<int> INTDEQUE;
    
    int read_integer(INTDEQUE &, int count);
    void write_integer(INTDEQUE &);
    void add_integers(INTDEQUE &sum, INTDEQUE X, INTDEQUE Y);
    void multiply_integers(INTDEQUE &product, INTDEQUE X, INTDEQUE Y, int count_A, int count_B);
    
    int main(void)
    {
    
    	INTDEQUE  A,B,S,P;
    	int count = 0, count2=0;
    	int count_A, count_B;
    
    	char quit = 'a';
    
    	do{
    
    	count_A = read_integer(A, count2);
    	count_B = read_integer(B, count2);
    
    	add_integers(S,A,B);
    	multiply_integers(P,A,B, count_A, count_B);
    
    	cout << endl;
    	cout << "The first integer is: ";
    	write_integer(A);
    	cout << "The second integer is: ";
    	write_integer(B);
    	cout << "The sum is: ";
    	write_integer(S);
    	cout << "The product is: ";
    	write_integer(P);
    
    	printf("\nQ - Quit\nC - Continue\n");
    	scanf("%c", &quit);
    	}while (quit != 'Q');  
    
    	return 0;
    }
    
    int read_integer(INTDEQUE &X, int count2)
    {
    	char line[100];
    	char * pline;
    
    	printf("Please type a large integer:\n");
    	gets(line);
    	fflush(stdin);
    
    	pline = line;
    
    	while(*pline != '\0')
    	{
    		if (*pline >='0' && *pline <= '9')
    		{
    			X.push_back(*pline -'0');
    			count2++;
    		}
    		pline++;
    	}
    
    	return count2;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is std::getline(cin, theString).

    Kuphryn

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The other is to read the FAQ.

    >>gets(line);
    Read me

    >>fflush(stdin)
    Read me
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    In old school C++ (Turbo C++ v1.01) Using gets you need to then convert the gets string to an integer using "atoi". I don't know if that command still exists in the newer C++ compilers. (I tried to use "atoi" from one of my older programs with my new Dev C++ and it didn't recognize the command...).

    Anyway, you could try it.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Using gets you need to then convert the gets string to an integer using "atoi".
    Again, this is all in the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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