why is this happening?

My problem is the loop below is repeating twice the second time the code is executed
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef UNICODE
#define UNICODE
#endif

#ifndef _UNICODE
#define _UNICODE
#endif

#include <windows.h>
#include <iostream>
#include <string>
#include <tchar.h>

int main()
{
	SetConsoleTitle(_T("Algebra I Console Window Calculator"));
/*right here*/	for (int Counter=0;Counter!=1;){
		// The Begining Of The Code, Depending on What Letter Is Entered It Will Change The Type Of Equation
		std::string Problem;
		std::cout<<"Enter The Chapter Of The Problem (Type Help For A List): ";
		getline (std::cin,Problem);
	
		//A List of Options
		if (Problem=="Help"||Problem=="help"){ 
			std::cout<<std::endl;
			std::cout<<"1.2 = Exponents and Powers"<<std::endl;
			std::cout<<"3.1 = Solving Equations Using Addition and Subtraction"<<std::endl;
			std::cout<<"9.1 = Solving Quadratic Equations by Finding Square Roots"<<std::endl;
			std::cout<<"9.6 = Applications of the Discriminant"<<std::endl;
			std::cout<<"10.7 = Factoring Special Products"<<std::endl;
			std::cout<<"Help = Display Options"<<std::endl;
			std::cout<<"Close = Closes The Program"<<std::endl;
			std::cout<<std::endl;}

	//Closes The Program Properly
		if (Problem=="Close"||Problem=="close"){
			return 0;}
		
		//Exponents and Powers(Section 1.2)
		if(Problem=="1.2") {
			double Base;
			double Exponent;
			std::cout<<"Enter The Base: ";
			std::cin>>Base;
			std::cout<<"Enter The Exponent: ";
			std::cin>>Exponent;
			double Result (1);
			for (int Counter2=0;Counter2<Exponent;Counter2++){
				Result*=Base;}
			std::cout<<Result<<std::endl;}
		
		//Solving Equations Using Addition and Subtraction(Section 3.1)
		if(Problem=="3.1"){
			double C;
			double B;
			std::cout<<"Enter It In The Format A+B=C"<<std::endl;
			std::cout<<"Enter B's Value: ";
			std::cin>>B;
			std::cout<<"Enter C's Value: ";
			std::cin>>C;
			double A(C-B);
			std::cout<<"A Equals: "<<A<<std::endl;}

		//Solving Quadratic Equations by Finding Square Roots(Section 9.1)
		if(Problem=="9.1"){
			int Correct_Result;
			int Testing_Number(0);
			std::cout<<"Enter The Perfect Square To Find The Root Of: ";
			std::cin>>Correct_Result;
			for(double Test_Result=0;Test_Result!=Correct_Result;){
				Testing_Number++;
				Test_Result=Testing_Number*Testing_Number;}
			std::cout<<"The Square Root Is "<<Testing_Number<<std::endl;}


		//Applications of the Discriminant(Section 9.6)
		if(Problem=="9.6") {
			int B;
			int A;
			int C;
			std::cout<<"We Are Going To Calculate The Amount Of Solutions And X Intercepts"<<std::endl;
			std::cout<<"Enter B's Value: ";
			std::cin>>B;
			std::cout<<"Enter A's Value: ";
			std::cin>>A;
			std::cout<<"Enter C's Value: ";
			std::cin>>C;
			int Answer(B*B-4*A*C);
			std::cout<<Answer<<std::endl;
			if (Answer>0) {
				std::cout<<"There Are 2 Solutions And 2 X Intercepts."<<std::endl;}
			if (Answer==0){ 
				std::cout<<"There is 1 Solution And 1 X Intercept."<<std::endl;} 
			if (Answer<0){
				std::cout<<"There Are No Solutions Or X Intercepts."<<std::endl;}}

		//Factoring Special Products(Section 10.7)
		if(Problem=="10.7"){
			double a;
			double b;
			std::string Format;
			std::cout<<"What Format Is The Problem In"<<std::endl;
			std::cout<<"1 = a^2-b^2"<<std::endl;
			std::cout<<"2 = a^2+2ab+b^2"<<std::endl;
			std::cout<<"3 = a^2-2ab+b^2"<<std::endl;
			std::cout<<":";
			getline(std::cin,Format);
			if(Format=="1"){
				std::cout<<"Enter a: ";
				std::cin>>a;
				std::cout<<"Enter b: ";
				std::cin>>b;
				std::cout<<"("<<a<<"+"<<b<<")("<<a<<"-"<<b<<")"<<std::endl;}
			if(Format=="2"){
				std::cout<<"Enter a: ";
				std::cin>>a;
				std::cout<<"Enter b: ";
				std::cin>>b;
				std::cout<<"("<<a<<"+"<<b<<")^2"<<std::endl;}
			if(Format=="3"){
				std::cout<<"Enter a: ";
				std::cin>>a;
				std::cout<<"Enter b: ";
				std::cin>>b;
				std::cout<<"("<<a<<"-"<<b<<")^2"<<std::endl;}
		}
	}
}

the output is displayed as... well for example if I enter 9.1

Enter The chapter Of The Problem (Type Help For a List): 9.1
Enter The Perfect Square To Find The Root Of: 4
The Square Root Is 2
Enter The Chapter Of The Problem(Type Help For A List):Enter The Chapter Of The Problem(Type Help For A List):

instead of
Enter The chapter Of The Problem (Type Help For a List): 9.1
Enter The Perfect Square To Find The Root Of: 4
The Square Root Is 2
Enter The Chapter Of The Problem(Type Help For A List):

but it only happens when I enter numbers at the beginning. If I enter Any words it works fine
Last edited on
sorry but bump
Your using cin >> throughout, it's probably a left over '\n' once the current chapter is completed.
change line 20 to:

1
2
3
4
cin.ignore(1000,'\n');
std::cout<<"Enter The Chapter Of The Problem (Type Help For A List): ";
		getline (std::cin,Problem);
Last edited on
that works but it isn't any better because then i have to press enter at the very beginning of the program
Last edited on
ok well in that case remove it, and place it at every instance where you read in via "cin"

so on lines: 46, 60, 69, 87, 124. Place it after the use of cin.

OR change all cin calls to getline() and parse to ints and doubles using a stream function :P

1
2
3
4
5
bool gotInteger(const std::string& str, double& d)
{
   std::stringstream ss(str);
   return ss >> d ? true : false;
}
Last edited on
thanks adding std::cin.ignore(); at the end of each if/loop made it work perfect. thanks for the help with figuring out the issue!
Topic archived. No new replies allowed.