nested if loop not working correctly

Hello all, I apologise if my question seems stupid but I have just recently started coding in c++ and this has been driving me crazy for hours. I have searched the web and experimented with different coding but cant seem to get it to work.

the nested if loop inside the while loop to check if the name is correct is just completely skipped whenever the program is run. it compiles fine and all that and I am just really confused as to why my program stops reading anything after this line of code int x = intInput(); and just goes back to the start of the while loop as the correctName bool is still set to false regardless of whether the user inputs 1 to indicate its correct.


any help will be greatly appreciated.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  #include "stdafx.h"
#include "iostream"
#include <string>
using namespace std;



// Forward decleration of add function.
double add(double x, double y); 

// Forward decleration of subtract function.
double subtract(double x, double y);

//Forward decleration of multiply function.
double multiply(double x, double y);

//Forward decleration of divide function.
double divide(double x, double y);

// Forward decleration of doubleInput function.
double doubleInput();

//Forward decleration of stringInput function.
string stringInput();

//Forward decleration of intInput function.
int intInput();

//Boolean to declare main function is complete or not.
bool isFin = false;

string name;

int main()
{
	// boolean to declare if name has been entered correctly or not
	bool correctName = false;

	// While name is incorrect
	while (correctName == false)
	{
		cout << "Hello, user! Please enter your name..." << endl;

		name = stringInput();

		cout << "You entered: " << name << ".\nIs this correct?\n1 - Yes.\n2 - No." << endl;


		int x = intInput(); // code stops being read after this

		if (x == 1)
		{
			cout << "Perfect, lets continue!" << endl;
			correctName = true;
		}

		else if (x == 2)
		{
			cout << "Lets try again." << endl;
		}

		else
		{
			cout << "Please enter a correct value." << endl;
		}
	}

	while (isFin == false)
	{

	}
    return 0;
}

double add(double x, double y)
{
	return x + y;
}

double subtract(double x, double y)
{
	return x - y;
}

double multiply(double x, double y)
{
	return x*y;
}

double divide(double x, double y)
{
	return x / y;
}

double doubleInput()
{
	double x;
	cin >> x;
	return x;
}

string stringInput()
{
	string x;
	cin >> x;
	return x;
}

int intInput()
{
	int x;
	cin >> x;
	return x;
}
Last edited on
Here's what the output that I get when I run your program:
Hello, user! Please enter your name...
dave
You entered: dave.
Is this correct?
1 - Yes.
2 - No.
1
Perfect, lets continue!

At that point nothing happens. The reason is that the code is in the infinite loop from lines 68-71. So I think the program is running correctly, you just have to add whatever is supposed to happen inside that loop.
This is what happens when I run the program...

Hello, user! Please enter your name...
David
You entered: David.
Is this correct?
1 - Yes.
2 - No.
1
Hello, user! Please enter your name...

What you get is what i expect at this stage
Okay so I got it working. I had just saved the .cpp file outwith the project folder and it couldnt find the exe. sorry for the stupidity guys
Topic archived. No new replies allowed.