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;
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....)
// with one dollar.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
usingnamespace 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();}
elseif(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();}
elseif(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
elseif(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:
#include <iostream>
usingnamespace 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
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';}