Program skips input?

I'm creating a program that inputs a math function, and inputs a number for x, and then solves the function.

When I enter in a function, it skips over the loop twice before asking me what X is. No matter what I enter, it comes back with an answer of 0. Help?

Here is my 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
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

int main()
{

	bool done = false;

	while (done != true)
	{
			int Continue = 1; 
			int answer = 0;
			int x = 0;
			int function = 0;
			cout << "Type in the function. \nPlease enter things like \n20x as 20 * x, and \nexponents like 9x^2 as 9 * x^2";
			cout << endl << endl;
			cin >> function;
			
			// is cin in a fail state?

			if (!cin)
			{
				cin.clear();
				cin.ignore();
			}
			
			cout << "What is x? ";
			cin >> x;

			cout << "\n\nSolving..." << endl;
			answer = function;

			cout << "Answer is " << answer << endl;

			cout << endl << "Continue? 1 for yes, 2 for no.";
			cin >> Continue;
			if (Continue == 1)
				continue;
			else if (Continue == 2) {
			done = true;
			}

	}




}
You can't store strings in integers. Cin does not automagically convert 'functions' to their results, you have to do this yourself. In other words, you have to write the code to parse the function tokens an evaluate them in (P)(E)(MD)(AS) fashion.
Last edited on
So I've gotta store the values in separate integers and then combine them into one variable?
Yes, you also have to use chars to hold the '*' and '^' and 'x', etc...
It won't work unless you space everything, though...
I don't think I'd have to hold the * and ^ in chars, as those are part of math functions included in C++.
How would you know that the user entered them? ;p
Topic archived. No new replies allowed.