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;
}