4 Replies - 280 Views - Last Post: 27 October 2013 - 12:50 PM Rate Topic: -----

#1 jjjk29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 07-October 13

Returning a char with a switch only works once?

Posted 27 October 2013 - 12:35 PM

I am trying to return a character from a function. The codes works fine for the first run of the switch. So if I hit M then the function will repeat. Then if I hit Q the program will close. The problem is I want this function to repeat until Q is hit.

The first run of the switch I hit M then on the second I hit D (for example) but the window will close and I get an error stating: ***Run-Time Check Failure #3 - The variable 'choice' is being used without being initialized.*** I can click Break or Continue. If I click continue the window will reappear and I can hit Q to close but if I hit M, D, or H I get the same error.

Any tips? I think my code is alright but if I move the if statement to main I get the same error as above.

 
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

char menu();
void mostPowerFul(double, double, double);

int main()
{
	char choice;
	
	menu();
	do
	{
	menu();
	}
	while (choice != 'Q');

	cin.get();
	cin.get();
	return 0;
}

char menu()
{
	char choice;
	cout << "Welcome to Jacob Cooper's Space Travel Company" << endl;
	cout << "(M)ost Powerful Calculation" << endl;
	cout << "(D)iscount Calculation" << endl;
	cout << "(H)ow Many Calculations" << endl;
	cout << "(Q)uit" << endl;
	cout << "Please enter the option (M,D,H, or Q)" << endl;
	cin >> choice;
	
	switch (choice)
	{
	case 'M': cout << "Some code for M" << endl;
		break;
	case 'D': cout << "Some code for D" << endl;
		break;
	case 'H': cout << "Some code for H" << endl;
		break;
	case 'Q': cout << endl;
		break;
	}
		if (choice == 'Q')
	{
		exit(0);
	}
	return choice;

}

void mostPowerFul(double val1, double val2, double val3)
{
	cout << "Please enter 3 power output measurements in MW: " << endl;
	cin >> val1 >> val2>> val3;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Returning a char with a switch only works once?

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1770
  • View blog
  • Posts: 3,428
  • Joined: 30-May 10

Re: Returning a char with a switch only works once?

Posted 27 October 2013 - 12:45 PM

> error stating: ***Run-Time Check Failure #3 - The variable 'choice' is being used without being initialized.***
Well the choice in main() has nothing to do with the choice in menu().

> 52 return choice;
You have this is menu(), but you don't catch the result in main.
Perhaps if you did this in main....

choice = menu();
Was This Post Helpful? 0
  • +
  • -

#3 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,409
  • Joined: 13-June 08

Re: Returning a char with a switch only works once?

Posted 27 October 2013 - 12:45 PM

You have two different choice variables.

The one in main is one variable named choice that is a char.

The one in your function Menu is another variable named choice that is a char.

I imagine what you actually wanted to do was set choice equal to Menu and have menu return a char for choice. Instead, you're declaring choice in main and not initializing it which causes the error. The program only fails after the second time because the menu code runs twice before the while is evaluated.

do{
choice = menu();
}while (choice != 'Q')



I imagine you meant to do that.

This post has been edited by ccubed: 27 October 2013 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 168
  • Joined: 27-May 13

Re: Returning a char with a switch only works once?

Posted 27 October 2013 - 12:47 PM

You don't do anything with the char you return.
You just say: menu();
You'll return a char, but you don't do anything with the returned char.
It gets thrown away.

the variable choice inside main is a different value than the choice inside menu.
So changing one will not change both.

So in main all that happens is:
char choice;

Then you check if choice != 'Q' but you never set choice

This post has been edited by axel1994: 27 October 2013 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 jjjk29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 07-October 13

Re: Returning a char with a switch only works once?

Posted 27 October 2013 - 12:50 PM

Hey thanks guys!

Simple fix I should have seen it! Thanks again!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1