Input not being passed to function back to main.

This is test function for a bigger program I'm working on. For some reason
it always skips straight to the else block in the string_test function as if
I didn't enter Y or N in main. Help?

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
//
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){char ch; cin>>ch;}

void string_test(string resume, bool entry){
	string bad_input = "???";
	if (resume == "Y"){
		entry = true;
	}
	else if (resume == "N"){
		entry = false;
	}
	else
		bad_input = resume;
		throw bad_input;
}

int main() { 
	try{
		bool entry = true;
		double num_input = 0;
		string resume = "???";
		vector<int>storage;
		while(entry == true){
			cout << "Enter a number to be stored." << endl;
			cin >> num_input;
			storage.push_back(num_input);
			cout << "Continue? Y/N" << endl;
			cin >> resume;
			string_test(resume, entry);
		}
	}
	catch(string bad_input){
		cout << bad_input << " is not Yes or No. Enter Y or N" << endl;	
	}
}
closed account (NyqLy60M)
Your vector "storage" is expecting integers, and you're attempting to pass in doubles.

As for it always acting as though you didn't respond 'Y' or 'N', the string_test function will always throw bad_input, because you don't have it blocked off. You're also passing in the value of entry (a copy of the variable), not the memory location itself. Look into using pointers.

Or, you could alternatively change the return type of string_test to int, and return 0 when resume == "Y", and 1 when resume == "N" and use that number accordingly.
Last edited on
This has worked in a different program I was working on. Is there any reason why it works in this one? Sorry for being curious.

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
// Quadratic Formula--- b^2-4ac must not = 0.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){char ch; cin>>ch;}
 
 
 
double sqrt(double x);


void calculate(double a, double b, double c, double answer1, double answer2){

	string root_test = "bad";
	double root;
	root = b*b-4*a*c;

	answer1 = (-b + sqrt(b*b-4*a*c)) / (2*a);
	
	answer2 = (-b - sqrt(b*b-4*a*c)) / (2*a);
	
	if (root <= 0){
		throw root_test;
	}
	
	cout << "Roots are " << answer1 << " and " << answer2;
	
}

int main() { 
	try{

		double a;
		double b;
		double c;
		double answer1;
		double answer2;
		
		cout << "The value of a is..." << endl;
		cin >> a;
		cout << "The value of b is..." << endl;
		cin >> b;
		cout << "The value of c is..." << endl;
		cin >> c;
		
		calculate(a, b, c, answer1, answer2);
		
		return 0;
	}
	catch(string root_test){
		cout << "The root of a quadratic formula cannot be less than or equal to 0" << endl;	
	}
	
}
Last edited on
closed account (NyqLy60M)
Like I already mentioned, take a look at the else condition in your string_test function. Even if resume is "Y", it's still going to throw bad_input.

And line 19 in your second program can be used in lines 21 and 23.
Topic archived. No new replies allowed.