GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Mar-2008, 18:25
ms080tx ms080tx is offline
New Member
 
Join Date: Mar 2008
Posts: 3
ms080tx is on a distinguished road

Homework help with looping


Hi my assignment is simple I was to display a menu and then build functions that would do various things based on the user selection. my problem is I'm trying to have a do loop that will display the menu function display_menu until the q (quit opintion) is selected. What I want my loop to do is to is to keep displaying the menu function unitl q is selected then quite and exit the program. here are the area of codes I believe need some kind of fixing.
CPP / C++ / C Code:
char display_menu(void);

void main (void)
{
	
	do
	{
		display_menu();
	}
	while (display_menu() != 'Q' || display_menu() !='q');
		
	}
char display_menu (void)
 {
	int num1;
	int num2;
	char userchoice;
	

	cout << "Command Options\n---------------------------------------" << endl;
	cout << "a : Is sum even" << endl;
	cout << "b : sum between two integers" << endl;
	cout << "c : Prime" << endl;
	cout << "d : Reverse" << endl;
	cout << "q : Quit" << endl;
	cout << "Enter a command a,b,c or q" << endl;
	cin >> userchoice;
	
	switch (userchoice)
	{
	case 'a':
		cout << "Enter first number\t: ";
		cin >> num1;
		cout << "Enter second number\t: ";
		cin >> num2;
		cout << isEven(num1,num2); cout << endl;
		break;
	case 'b':
		cout << "Enter the first number for the sum between two integers : ";
		cin >> num1;
		cout << "Enter the second number for the sum between two integers : ";
		cin >> num2;
		sum(num1,num2); cout << endl ;
	
		break;
	case 'c' :
		cout << "Enter number : ";
		cin >> num1;
		isPrime(num1); cout << endl;
		break;
	case 'd' :
		cout << "Enter a positive number : ";
		cin >> num1;
		reverse(num1);cout << endl ;
		
		break;
	case 'q' :
		cout << "thank you come again" << endl;
		break;

	default :
		cout << "invalid choice";cout << endl;
		
	}
	return userchoice;

 }
I would like to solve my menu question using some kind of loop as basically all we have learned is loops and functions and simple operators. We are only about about half way or so in the semester for my programming class so try to keep it simple with any suggestions you may have. There are no compling errors but as I said before what is should be doing: displaying the menu unitl the user inputs q and then exiting the loop is not happening. Instead it cycles through the loop endless. Another quirky thing it does is that after entering Q
if on the next user entry is q is entered it will exit the program. Thank you for your time and help. to compile the program I'm using Microsoft visual studio 2005 on a windows XP o/s
  #2  
Old 20-Mar-2008, 19:08
C++_Bandit's Avatar
C++_Bandit C++_Bandit is offline
Junior Member
 
Join Date: Mar 2008
Location: Virginia
Posts: 40
C++_Bandit will become famous soon enough

Re: Homework help with looping


First off, do you have to use Microsoft Visual Studio 2005? If not then STOP, instead use either Code::Blocks or Bloodshed Dev C++ or any other free compiler.

Second, do you mind posting your entire code? I don't know how you're invoking the loop or getting the new choice.
  #3  
Old 20-Mar-2008, 20:23
ms080tx ms080tx is offline
New Member
 
Join Date: Mar 2008
Posts: 3
ms080tx is on a distinguished road

Re: Homework help with looping


here is the entire code:
CPP / C++ / C Code:
/* HW 4
Tuesday lab 1:40 */
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
//prototype for functions
	bool isEven(int,int);
	int sum(int,int);
	bool isPrime(int);
	void reverse(int);
	char display_menu(void);

void main (void)
{
	
	do
	{
		display_menu();
	}
	while (display_menu() != 'Q' || display_menu() !='q');
		
	}
bool isEven (int number1, int number2)
{
	
	if ((number1 + number2)%2 ==0)
	{
		cout << " the sum of the numbers is even\t" << endl;
		return true; 
	}
	else 
	{
		cout << "the sum of the numbers is odd\t" << endl;
		return false;
	} 
		
	
}
 int sum (int number1, int number2)
 {
	 
	 int i,total=0;
	 
	if (number1>number2)
	 {
		for(i=number1; i >=number2; i--)
			total = total + i;
			cout << "sum of series is "<< total;
	 }
	else
	{
		for(i= number2; i >= number1; i--)
			total= total + i;
			cout << "sum of series is "<< total;
	}
	return total; 
 }
 bool isPrime(int number1)
 {
	if(number1== 2 || number1 == 3 || number1 == 5 || number1 == 7)
	{
		cout << "number is prime" << endl;
		return true;
	}
	else 
	{
		if ( number1%2== 0 || number1%3 == 0 || number1%5== 0 || number1%7== 0)
		{
			cout << "number is not prime" << endl;
			return false;
		}
		else 
		{
			cout << "number is prime" << endl;
			return true;
		}
	}
 }
 void reverse (int number1)
 {
	 if( number1 > 0 )
    {
        do
        {
            cout << (char)( '0' + (number1 % 10) );
            number1 /= 10;
        } while( number1 != 0 );
        cout << endl;
    }
	 else 
		 cout << "fool I said enter a positive number" << endl;

 }
 char display_menu (void)
 {
	int num1;
	int num2;
	char userchoice;
	

	cout << "Command Options\n---------------------------------------" << endl;
	cout << "a : Is sum even" << endl;
	cout << "b : sum between two integers" << endl;
	cout << "c : Prime" << endl;
	cout << "d : Reverse" << endl;
	cout << "q : Quit" << endl;
	cout << "Enter a command a,b,c or q" << endl;
	cin >> userchoice;
	
	switch (userchoice)
	{
	case 'a':
		cout << "Enter first number\t: ";
		cin >> num1;
		cout << "Enter second number\t: ";
		cin >> num2;
		cout << isEven(num1,num2); cout << endl;
		break;
	case 'b':
		cout << "Enter the first number for the sum between two integers : ";
		cin >> num1;
		cout << "Enter the second number for the sum between two integers : ";
		cin >> num2;
		sum(num1,num2); cout << endl ;
	
		break;
	case 'c' :
		cout << "Enter number : ";
		cin >> num1;
		isPrime(num1); cout << endl;
		break;
	case 'd' :
		cout << "Enter a positive number : ";
		cin >> num1;
		reverse(num1);cout << endl ;
		
		break;
	case 'q' :
		cout << "thank you come again" << endl;
		break;

	default :
		cout << "invalid choice";cout << endl;
		
	}
	return userchoice;

 }
  #4  
Old 21-Mar-2008, 03:23
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 348
Peter_APIIT is on a distinguished road

Re: Homework help with looping


Use int main instead of void because this is not a standard C++ code.
__________________
Linux is the best OS in the world.
  #5  
Old 21-Mar-2008, 13:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,506
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Homework help with looping


Quote:
Originally Posted by ms080tx
here is the entire code:

Without looking at the functions other than main(), here's a suggestion:

In your loop, you should call the display_menu() once. Save its value to use as loop control.

Also look at the logic to decide when to continue: If the choice is not equal to 'Q' and the choice is not equal to 'q' you will continue.

So your version of main() could look like:
CPP / C++ / C Code:
int main(void)
{
    char choice;

    do 
    {
        choice = display_menu();
    }
    while (choice != 'Q' && choice != 'q');

    return 0;
}

Regards,

Dave
  #6  
Old 21-Mar-2008, 17:53
ms080tx ms080tx is offline
New Member
 
Join Date: Mar 2008
Posts: 3
ms080tx is on a distinguished road

Re: Homework help with looping


thanks this seems to have fixed the problem!!!! that one little change from || to && and the few other changes fixed it. Thanks again for your help
 

Recent GIDBlogGoing to Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Input stream question earachefl CPP / C++ Forum 17 26-Sep-2007 20:21
New to programming (Homework Help) fishguts Miscellaneous Programming Forum 1 12-Sep-2007 10:50
Code for Homework bobbiegirl Java Forum 2 26-Feb-2007 20:34
Need help with Looping Harryt123 C Programming Language 4 21-May-2006 14:45
Need Help W/ Homework Due Tomorrow ilovethebeach C Programming Language 7 10-Sep-2005 14:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.