do while loop

im currently doing a programmme. problem is i'm unabel to allow the programme to run continuosly. any ideas? the program is a follows:

int _tmain(int argc, _TCHAR* argv[])
{


int iInput;
char a;
bool Y;
bool N;

// cin >> Y;

N = 1;
Y = 0;


srand ( time(NULL) );//cout << "Enter a number between 0 and 9: ";
int random = rand()% 100 ;//

do
{

cout << "\nPlease enter the amount $1, $2,$5: ";
cin >> iInput;
{

switch(iInput)

{//start of switch case
case 1: for (a=0; a<2; a++)
{
cout <<"\nPlease enter a number: ";
cin >> iInput;

{
if (iInput < random)
{
cout <<"\nEntered Number is lower than value, retry with a higher number";
cin.ignore();
}
else if(iInput > random)
{cout <<"\nEntered Number is higher than value, retry with a lower number";
cin.ignore();
}
else if(iInput == random)
{
cout <<"\nCongratulations, you have guess the number correctly!!";
system("PAUSE");
cin.get();
}}
}
if(iInput < random)
{
cout << "\nSorry, you have exhausted all your tries...";
cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
}
else if(iInput > random)
{
cout << "\nSorry, you have exhausted all your tries...";
cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
}
else(iInput == random);
{
//cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
}
break;



case 2:for (a=0; a<5; a++)
{
cout <<"\nPlease enter a number: ";

cin >> iInput;
{
if (iInput < random)
{{
cout <<"\nEntered Number is lower than value, retry with a higher number";
cin.ignore();}

}
else if(iInput > random)
{
cout <<"\nEntered Number is higher than value, retry with a lower number";
cin.ignore();
}
else if(iInput == random)
{
cout <<"\nCongratulations, you have guess the number correctly!!";
system("PAUSE");
cin.ignore();
}}
}
if(iInput < random)
{
cout << "\nSorry, you have exhausted all your tries...";
cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
}
else if(iInput > random)
{
cout << "\nSorry, you have exhausted all your tries...";
cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
}
else(iInput == random);
{
//cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
}
break;


case 5: for (a=0; a<13; a++)

{
cout <<"\nPlease enter a number: ";
cin >> iInput;
{
if (iInput < random)
{
cout <<"\nEntered Number is lower than value, retry with a higher number";
cin.ignore();

}
else if(iInput > random)
{
cout <<"\nEntered Number is higher than value, retry with a lower number";
cin.ignore();

}
else if(iInput == random)
{
cout <<"\nCongratulations, you have guess the number correctly!!";
cin.get();
system("PAUSE");
cin.get();
}}
if(iInput < random)
{
cout << "\nSorry, you have exhausted all your tries...";
cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
return 0;
}
else if(iInput > random)
{
cout << "\nSorry, you have exhausted all your tries...";
cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
}
else(iInput == random);
{
//cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
}break;
default : cout <<"Please enter $1, $2 or $5 only...\n";
break;
cin.ignore();
return 0;

}//end of switch case

Y++;
cin.ignore();
return 0;

}} while (Y<10);
Is there a reason that the code is not in [code][/code] tags? And also, is there a reason that the while (Y<10); is outside of main()?
Last edited on
@LB:the reason being is i don't know how to use tags. the while(Y<10) is on the basis on trail error

@vexer:thanks?
I don't mean to sound rude, but no one is going to help you until you clean up your code and make it readable.
ok. cleaned up aldy:

Im nw doing it part by part:

// with one dollar.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
using namespace std;


int main()
{
int iGuess,oneTry, finalTry, trialGuess,finalGuess;//a, i,
bool Y;
bool N;

N = 1;
Y = 0;

srand ( time(NULL) );//cout << "Enter a number between 0 and 9: ";
int random = rand()% 100 ;//


cout<<"n\Please enter the amount, $1, $2 or $5:";
cin >> iGuess;

switch(iGuess)
{//switch case start
case 1: if (iGuess=random)
{
cout <<"\nCongratulations, you have guess the number correctly!!";
system("PAUSE");
cin.get();
}
else if (iGuess!=random)
{
oneTry = trialGuess;
finalTry= finalGuess;
}//case 1 end
}//switch case end (case 2, case 5 to be done seperately then combined ltr)


}////end of main


int trialGuess(int iGuess,int random)
{
if(iGuess<random)
{
cout <<"\nEntered Number is lower than value, retry with a higher number";
cin.ignore();
}
else if(iGuess>random)
{
cout <<"\nEntered Number is higher than value, retry with a lower number";
cin.ignore();

}

}


int finalGuess(int iGuess, int random)
{
if(iGuess<random)
{
cout <<"\nEntered Number is lower than value, retry with a higher number";
cout << "\nSorry, you have exhausted all your tries...";
cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";
cin.ignore();} - A

else if(iGuess>random);
{cout <<"\nEntered Number is higher than value, retry with a lower number";
cout << "\nSorry, you have exhausted all your tries...";
cout <<"\nEnter 'N' or 'n' to exit, any other key to continue..:";}B

}

Problem is:
1) still unable to do a 'do while loop' for A and B
2) ifor error: 1>c:\users\acer\documents\visual studio 2008\projects\with one dollar\with one dollar\with one dollar.cpp(79) : error C4716: 'finalGuess' : must return a value
(idk what is a return....)
you forgot to put it in [code][/code] tags. You need to put it in those tags to look like this:
1
2
3
int main(){
	//my code here
}
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
// with one dollar.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
using namespace std;

int main(){
	int iGuess,oneTry,finalTry,trialGuess,finalGuess;//a,i,
	bool Y=0;
	bool N=1;
	srand(time(NULL));//cout<<"Enter a number between 0 and 9: ";
	int random=rand()%100;
	cout<<"n\Please enter the amount,$1,$2 or $5:";
	cin >> iGuess;
	switch(iGuess){
		case 1:
			if(iGuess=random){
				cout<<"\nCongratulations,you have guess the number correctly!!";
				system("PAUSE");
				cin.get();}
			else if(iGuess!=random){
				oneTry=trialGuess;
				finalTry=finalGuess;}}}

int trialGuess(int iGuess,int random){
	if(iGuess<random){
		cout<<"\nEntered Number is lower than value,retry with a higher number";
		cin.ignore();}
	else if(iGuess>random){
		cout<<"\nEntered Number is higher than value,retry with a lower number";
		cin.ignore();}}

int finalGuess(int iGuess,int random){
	if(iGuess<random){
		cout<<"\nEntered Number is lower than value,retry with a higher number";
		cout<<"\nSorry,you have exhausted all your tries...";
		cout<<"\nEnter 'N' or 'n' to exit,any other key to continue..:";
		cin.ignore();}//A
	else if(iGuess>random){
		cout<<"\nEntered Number is higher than value,retry with a lower number";
		cout<<"\nSorry,you have exhausted all your tries...";
		cout<<"\nEnter 'N' or 'n' to exit,any other key to continue..:";}}//B


To make it like this ^^^, put [ code ] and [ /code ] (without the spaces) around your code.

1.) I don't understand what you mean...
2.) A return value is the variable that a function gives to the program. In finalGuess, you might return void (basically nothing). Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int multiply(int num1,int num2){
	return num1*num2;}//return the two numbers multiplied

int main(){
	int num1,num2;
	cout<<"First Number: ";
	cin>>num1;
	cout<<"Second Number: ";
	cin>>num2;
	cout<<num1<<" * "<<num2<<" = "<<multiply(num1,num2);
	//in the above line, we output what "multiply" returns,
	//in this case, the product of the two numbers. Basically,
	//return replaces the function in the line with what you return
	cin.get();
	return 0;}//here, we let the OS know that we're done, and everything
	//went OK

Last edited on
piMaster:thanks for your help, i've managed to correct some of the errors... however there still some more problems faced. such as old style format list: this is the overall code,i've managed to come up with.


// 5th try.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <time.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int firstTry, finalTry,firstChoice,lastChoice;
int iInput;
int price[]={1,2,5};
//const int PRICE[]=3;
//int price[PRICE]={1, 2, 5};

char a;
char cont='Y';
srand(time(NULL));
int random=rand()%100;//0 -100

while (cont!='n' && cont!='N')
{
cout << "\n Please eneter the amount $1, $2,$5:";
cin >> price;

cout <<"\n Please enter a number:";
cin >> iInput;


switch(iInput)
{
case 1: firstChoice=firstTry;
lastChoice=finalTry;
break;
cin.ignore();


case 2: firstTry();
firstTry();
firstTry();
firstTry();
finalTry();
break;
cin.ignore();


case 5: for(a=0;a<12;a++)
{firstTry();}
finalTry();



default: cout<<"\n Please enter $1,$2, $5:";
break;
cin.ignore();
return 1;


}

}
return 0;
}



int firstTry (int iInput,int random,char 'Y')
{
int iInput;

if (iInput < random)
{cout << "\nEntered number is lower than value, retry with a higher number";}

else if(iInput > random)
{cout << "\nEntered number is higher than value, retry with a lower number";}

else if(iInput == random)
{cout << "\nCongratulations you have guessed the correct number";}

return 'Y';}




int finalTry(int iInput,int random)
{


if(iInput<random)
{
cout<<"\nEntered value is lower than value, retry with a higher number";
cout<<"\nSorry you have exhausted all your tries.....";
cout<<"\nEnter 'N' or 'n' to exit, any other key to continue....:";
}

else if(iInput>random)
{
cout<<"\nEntered value is higher than value, retry with a lower number";
cout<<"\nSorry you have exhausted all your tries.....";
cout<<"\nEnter 'N' or 'n'to exit, any other key to continue:";
}
else if(iInput==random)
{
cout<<"\nCongratulations, you have guessed the number correctly!!!! ";
cout<<"\nEnter 'N' or 'n' to exit, any other key to continue....:";
}
return 'Y';}
Topic archived. No new replies allowed.