Function not being called from my main function

Pages: 12
Hi all,

I'm writing a function getInt, which takes in an integer as an argument and prompts the user for input. If the user does not enter an integer, the getInt function will prompt the user again to enter an int onto the input buffer. My thinking was to check to see if I had valid input, if I did not have valid user input, I would flush the input buffer and prompt the user once again for input. The code is as follows:

void getInt(int &var) {
string empty;
cout << "enter an int-"<<endl;
cin >> var;
while(!cin.good()){
cin.clear();
getline(cin, empty);
cout << "You did not enter an int. Please enter an int: "<< endl;
cin>>var;

}
}

The problem:

I call this function from within my main function and it is not reached. I have no compiler errors (g++ compilation). I have tested this theory by putting an exit(1) at the beginning of this function. This function never gets called from main.

This is what my main funcion looks like:

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void check(bool isGood, string message, string fileName, string endLine);

void check(bool isGood, string message);

void getInt(int &var);

int main(){
ofstream output;
int n;
double x;
char ch;
bool yes;
string fileName;
ifstream input;
cout<<"Enter a name for the output file- ";
cin>>fileName;
output.open(fileName.c_str());
check(output.good(),"Failure to open the file '",fileName,"'");
input.open("input.txt");
check(input.good(),"Failure to open 'input.txt'");
input>>n;
while(input.good()){
output<<n;
input>>n;
}
cout<<"Enter an int- ";
cin>>n;
check(cin.good(),"'cin' has been disabled");
cout<<"You entered the int "<<n<<endl;
getInt(n);
cout<<"You entered "<<n<<endl;
}

Any input from anyone on these forums would be great. Thank you.


your check function doesn't have a body in your sample code.

what does your check function do?

also when posting a code please format it so it is easily readable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void check(bool isGood, string message, string fileName, string endLine);

void check(bool isGood, string message);

void getInt(int &var);

int main(){
ofstream output;
int n;
double x;
char ch;
bool yes;
string fileName;
ifstream input;
cout<<"Enter a name for the output file- ";
cin>>fileName;
output.open(fileName.c_str());
check(output.good(),"Failure to open the file '",fileName,"'");
input.open("input.txt");
check(input.good(),"Failure to open 'input.txt'");
input>>n;
while(input.good()){
output<<n;
input>>n;
}
cout<<"Enter an int- ";
cin>>n;
check(cin.good(),"'cin' has been disabled");
cout<<"You entered the int "<<n<<endl;
getInt(n);
cout<<"You entered "<<n<<endl;
} 
So, what happens? You program hangs? Finishes succesfully? Finishes unsucsessfully?
What does the "not reached" mean? Which is the last output that you do get?

The while loop on lines 29-32 continues until the input fails. You get out of the loop only with "bad" input. Then the check on line 35 becomes very interesting.
I mean that all of my functions are reached. It finishes successfully, it's just that the cout in the getInt method never prints out. This means that the cout function isn't actually being called by my main function. I'm sorry I didn't include the check before. I don't really know how to post code on this site, but here is my full code:

http://cpp.sh/7haq
the cout in the getInt method never prints out. This means that the cout function isn't actually being called by my main function.

Which cout? You have more than one. Do you not even see
enter an int-
displayed?

I don't really know how to post code on this site

Which means you haven't bothered to read any of the introductory info for new users. Look at:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
When you fix the syntax errors and run the program, note that the exit code is 1. This means check() is exiting. I think the problem is that cpp.sh doesn't display the contents of cerr. Change check() to the error message to cout and you'll see what I mean.
Works properly for me:
Enter a name for the output file- ss
Enter an int- 2
You entered the int 2
enter an int-
3
You entered 3

Process returned 0 (0x0)   execution time : 5.849 s
Press any key to continue.
What input do you enter?
Do you mean that both "You entered the int " and "You entered " do print at the end?
Sorry for being such a newbie. I was in a hurry on the morning post. I read the introductory info, I must've just missed the spot about posting code.

The output message that is not properly read is the lower case 'enter an int-' . Both "You entered the int" and "You entered" are printed at the end, however, I'm never prompted for input from the cout within the getInt function. And cout from the getInt function is never printed.

The check() functions both work properly. I am able to display the contents of cerr when I run into an error.
Look at my post, I will repost it and comment each output line (line numbers are from rafae11 post)
1
2
3
4
5
6
Enter a name for the output file- ss //output line 22 and input line 23
Enter an int- 2 //lines 33 and 34
You entered the int 2 //Line 36 in your code
enter an int- //from inside getInt on line 37
3 //input in getInt
You entered 3 //line 38 
What is the problem? Maybe it is just your IDE which won't hold window open after program finishes?
Last edited on
I'm not getting the same output as you. Mine completely skips "enter an int" Here is my output:

1
2
3
4
 Enter a name for the output file- input.txt
Enter an int- 5
You entered the int 5
You entered 5


I'm coding from a terminal and I'm using g++ as my compiler and emacs as my text editor.
That is interesting. Can you please post complete current compiling version of your code
Do you know how I find that information from the terminal? I'm not really sure where I can find that information on Unix.
Err, This is your code you wrote. That which you posted on cpp.sh is not compete and cannot compile, so I suppose that it is not the version you currently use.
Last edited on
This is my complete code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void check(bool isGood, string message, string fileName, string endLine); 

void check(bool isGood, string message);

void getInt(int &var);



int main(){
  ofstream output;
  int n;
  double x;
  char ch;
  bool yes;
  string fileName;
  ifstream input;
  cout<<"Enter a name for the output file- ";
  cin>>fileName;
  output.open(fileName.c_str());
  check(output.good(),"Failure to open the file '",fileName,"'");
   input.open("input.txt");
check(input.good(),"Failure to open 'input.txt'");
    input>>n;
 while(input.good()){
    output<<n;
    input>>n;
  }
 cout<<"Enter an int- ";
  cin>>n;
  check(cin.good(),"'cin' has been disabled");
  cout<<"You entered the int "<<n<<endl;
  getInt(n);
  cout<<"You entered "<<n<<endl;
  /*
  getInt(n,2,10);
  cout<<"You entered "<<n<<endl;
  yes=getYorN(ch);
  cout<<"You entered '"<<ch<<"'\n";
  if(yes)
     cout<<"This means yes\n";
  else
    cout<<"This means no\n";
  x=0;
  getBool(yes);
  cout<<"You entered the bool "<<yes<<endl;
  getDouble(x);
  cout<<"You entered the double "<<x<<endl;

  */
}
void check(bool isGood, string message, string fileName,  string endLine) {

  if  (!isGood) {

    cerr<< message << fileName << endLine << endl;
    exit(1);
	}
}

void check(bool isGood, string message){

  check(isGood, message, '\0','\0');
  
}
  void getInt(int &var) {
    string empty;    
     cout << "enter an int-"<<endl;
   cin >> var;
   while(!cin.good()){
      cin.clear();
       getline(cin, empty);
      cout << "You did not enter an int. Please enter an int: "<< endl;
      cin>>var;

	}
    
}

  
I added a new function called getInt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using namespace std;

void check(bool isGood, string message, string fileName, string endLine); 

void check(bool isGood, string message);

void getInt(int &var);

void getInt(int &var, int lower, int upper);

int main(){
  ofstream output;
  int n;
  double x;
  char ch;
  bool yes;
  string fileName;
  ifstream input;
  cout<<"Enter a name for the output file- ";
  cin>>fileName;
  output.open(fileName.c_str());
  check(output.good(),"Failure to open the file '",fileName,"'");
   input.open("input.txt");
check(input.good(),"Failure to open 'input.txt'");
    input>>n;
 while(input.good()){
    output<<n;
    input>>n;
  }
 cout<<"Enter an int- ";
  cin>>n;
  check(cin.good(),"'cin' has been disabled");
  cout<<"You entered the int "<<n<<endl;
  getInt(n);
  cout<<"You entered "<<n<<endl;
  
  getInt(n,2,10);
  cout<<"You entered "<<n<<endl;
  /*
  yes=getYorN(ch);
  cout<<"You entered '"<<ch<<"'\n";
  if(yes)
     cout<<"This means yes\n";
  else
    cout<<"This means no\n";
  x=0;
  getBool(yes);
  cout<<"You entered the bool "<<yes<<endl;
  getDouble(x);
  cout<<"You entered the double "<<x<<endl;

  */
}
void check(bool isGood, string message, string fileName,  string endLine) {

  if  (!isGood) {

    cerr<< message << fileName << endLine << endl;
    exit(1);
	}
}

void check(bool isGood, string message){

  check(isGood, message, '\0','\0');
  
}
  void getInt(int &var) {
    string empty;    
     cout << "enter an int-"<<endl;
   cin >> var;
   while(!cin.good()){
      cin.clear();
       getline(cin, empty);
      cout << "You did not enter an int. Please enter an int: "<< endl;
      cin>>var;

	}
    
}

void getInt(int &var, int lower, int upper) {

  getInt(var);
  
  while (!((var >= lower) && (var <= upper))) {

    getInt(var);
  }
}
 


The output is as follows:

1
2
3
4
Enter a name for the output file- input.txt
Enter an int- 5.2
You entered the int 5
You entered 5


So now "You entered " is not getting printed
Your code is incorrect should fail
1
2
check(isGood, message, '\0','\0');
//          Same as NULL ↑    ↑ 
Creating string from null pointer is undefined.

Code is fine (after fixing that error) and is working properly for me. Maybe it is problem with, say, object files not being removed correctly? Or you compiling older version of the code?
You can try to create a completely new program in another directory and copy-paste your code.

Code works for me and online compilers:
http://coliru.stacked-crooked.com/a/259eda20270cd933
I want those values to be null. How would I define them if I wanted them to be empty strings?
Alright, I think I know what I did wrong. On the terminal, I wasn't compiling it correctly, so I wasn't updating my object code. Now I tried to compile it correctly and I ran into the null string error. So I think I've got it to work. I just need to fix the null values. How are you suppose to call null strings?
Pages: 12