while loop

Hey so i've started programming like 2 weeks ago when i started reading a book about it, and since i've learned some of the basics. So i tryed to make a program just to remember what i had learned and to practice a bit. So i did this with visual studio 2008:
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
#include "iostream"
#include "conio.h"
#include "string"
#include "time.h"
#include "windows.h"
#include "stdio.h"
#include "math.h"

using namespace std;

float square (float x)
{
	float result1 = x*x;
	return result1;
}
float cube (float x)
{
	float result2 = x*x*x;
	return result2;
}
void gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x; coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}


void setColor(int x)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
void main()
{
	char key=' ';
	int i, x=0,p=0, d=1, k=0;
	float j;
	string Username, Answer1, Answer2, option[10];
	option[1] = "find the square of a number";
	option[2] = "find the cube of a number";
	option[3] = "play tic tac toe";
	option[4] = "do some second degree equations";
	option[5] = "get asked some questions";

	cout<<"Hello, welcome to noobcraft"<<endl;
	cout<<"Plase write your username"<<endl;
	getline (cin, Username);
	
	cout<<"Your selected username is "<<Username<<endl;
	cout<<"You sure you want to continue?";
	getline (cin, Answer1);

	if (Answer1 == "Yes" || Answer1 == "yes" || Answer1 == "Y" || Answer1 == "Yeh" || Answer1 == "ye" || Answer1 == "y")
	{
		system ("CLS");
		cout<<"Welcome, "<<Username<<". This program is just a way of me trying to remember what i learned so far. And you will help me."<<endl;
		cout<<"Now, please tell me which one of this things you want to do."<<endl;
		
		while (k == 0)
		{	
			for (i=0; i<5; i++)
			{
				cout<<i+1<<"-I want to "<<option[i+1]<<endl;
			}
			cout<<"Write the number of the option";
			cin>>p;
			if (p = 1)
			{
				while (d == 1)
				{
					
					system("CLS");
					cout<<"you've chosen option "<<p<<","<<option[p]<<endl;
					cout<<"Write the value of the number: ";
					cin>>j;
					cout<<square(j)<<endl;
					Sleep(20);
					cout<<"Do you want to 1.Find the value of another number or 2.Do something else?"<<endl;
					cin>>d;
					if (d = 2)
					{
						system ("CLS");
						k=0;
						d==2;
					}
				}
			}
			if (p == 2)
			{
				k++;
				system("CLS");
				cout<<"you've chosen option "<<p<<","<<option[p]<<endl;
			}
			if (p == 3)
			{
			    k++;
				system("CLS");
				cout<<"you've chosen option "<<p<<","<<option[p]<<endl;
			}
			if (p == 4)
			{
				k++;
				system("CLS");
				cout<<"you've chosen option "<<p<<","<<option[p]<<endl;
			}
			if (p == 5)
			{
				k++;
				system("CLS");
				cout<<"you've chosen option "<<p<<","<<option[p]<<endl;
			}
			
			}
		}
	}


K now the problem im having. What i tryed to do with the while loop ther is so that if the answer to the option is "i want to do something else" it just restarts. And it does. My problem is, when it restarts, it presents the options as it should, but then if i write somethng, it simply keeps giving the same options again and again. I'm kinda a noob at this so dont flame me if i did a stupid mistake. thx to those who help.
PS: Obviously it's still lacking the other options. I had already done those options in previous practice but as i got stuck with a totally diferent issue idont know howto solve i kinda stopped there.
You need to reset to k = 0;

Also, within a few if statements, you have assignment, rather than comparison:
1
2
p = 1    //assignment
p == 1 //comparison 
i believe i did reset the value
1
2
3
4
5
6
7
8
9
	
cout<<"Do you want to 1.Find the value of another number or 2.Do something else?"<<endl;
cin>>d;
if (d == 2)
{
	system ("CLS");
	k=0;
	d=2;
}

about the if statements i got them right now. but it still keeps doing the same thing. it keeps reseting to the start but then only giving me the options, not actually executing them.
It may be a pain to rewrite it, but you could make this more efficient and clean by using a switch statement:

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
	bool exit = false;
	int option = 0;

	do
	{
		std::cout << "Options: ...." << '\n';

		std::cout << "Enter choice: ";
		std::cin >> option;

		switch( option )
		{
			case 1: //user input: 1
				//call function
				break; //exit switch

			case 2:
				//call function
				break; //exit switch

			case 3: //option for exit
				exit = true;
				break;

			default: //this is called when case is not matched with any above
				std::cout << "Invalid entry!\n";
				break;

		}
	}while( ! exit ); //while exit == false 


By doing this, the program will only exit when the user inputs 3.

A do/while loop will make sure the code is run once before checking the exit bool too, so even if exit = true by accident, the user will still see your program and get given a choice.
Last edited on
Topic archived. No new replies allowed.