Void function

Hi, I've been having trouble in applying the void function in my simple program and wanting to avoid using the goto statement. My main goal is when the user inputs Y or y after a case in the switch function, I want loop back to the beginning of the program. So basically its a try again process that I wanna make simple. I'm not sure if I am using the right function declaration. Can any of you state my errors in the void function? Thanks.

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <stdio.h>

using namespace std;
void displayCpp(int y_n);
main()
{

int num_1, num_2, op_1, input, sum, diff, prod, quo;
char y_n;

printf("Please enter two numbers...");cout<<endl;
printf("\nEnter the first number: ");
scanf("%d", & num_1);
printf("\nEnter the second number: ");


//Switch operation
cout<<"[1] - Adddition"<<endl;
cout<<"[2] - Subtraction"<<endl;
cout<<"[3] - Multiplication"<<endl;
cout<<"[4] - Division"<<endl;

START :
printf("Enter your desired operation: ");
scanf("%d", & op_1);
switch(op_1)
{
case 1 :
sum = num_1 + num_2;
cout<<"The sum of the two numbers is = "<<sum<<endl;
break;
case 2 :
diff = num_1 - num_2;
cout<<"The difference of the two numbers is= "<<diff<<endl;
break;
case 3 :
prod = num_1 * num_2;
cout<<"The product of the two numbers is = "<<prod<<endl;
break;
case 4 :
quo = num_1 / num_2;
cout<<"The quotient of the two numbers is = "<<quo<<endl;
break;

default : cout<<"Operation unknown!";
}

system("pause>0");
}

void displayCpp(int y_n)
{
cout<<"Do you want to try again(y/n)?: ";
cin>>y_n;
if (y_n == 'Y' || y_n == 'y')
{
system("cls");
goto START;
}
else if (y_n == 'N' || y_n == 'n')
{
system("pause>0");
}
}
Last edited on
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
What you want is called a "do while" loop: http://www.cplusplus.com/doc/tutorial/control/#dowhile . You'd replace the START : tag with do and the:
1
2
3
4
5
if (y_n == 'Y' || y_n == 'y')
{
system("cls");
goto START;
}

statement with
 
while(tolower(y_n) != 'y');


Also, don't do this: pause("pause>0" . That little message from the pause statement is meant to be there. Piping the output message to null like that makes the application look like it's locking up.
Topic archived. No new replies allowed.