|
 |
|
 |
10-22-2006, 03:33 AM
|
#1 (permalink)
|
|
krisl100
Join Date: Oct 2006
Location: Montreal
Posts: 34
|
Excerxise 3
Here is another excercise I found, I have been trying this for 2 days now, so far all I have is the menu, I really need help with this one. Thanks.
Use a menu system offering the user four options, A, B, C, and X. The menu is in a loop -- option X merely exits the program. Options A, B and C must be written in separate functions (in fact, Option A requires two functions). It is also recommended that the displaying of the menu be in a function of its own
Option A: Prompt the user to enter a positive whole number. Display the sum of all the odd numbers between 1 and that number (inclusive) in the format shown below. Use a for loop to sum the numbers, not a mathematical formula. Use a bottom-tested yes/no loop to continue getting numbers from the user.
Important: This option requires two functions –- a scaffold function and a calculation function. When Option A is chosen, the menu calls the scaffold function, which contains the yes/no loop -- it gets the user input, calls the calculation function, and then prints the result. It passes the user’s input to the calculation function (containing the for loop) which returns the result to the scaffold function for printing. No validation of the input is required.
Enter number: 13
Total of all odd numbers between 1 and 13: 49
Continue (Y/N)? Y
Enter number: 14
Total of all odd numbers between 1 and 14: 49
Continue (Y/N)? N
Option B: An employee begins working at a salary of $0.01 per day. Each day her daily salary doubles ($0.02 on the 2nd day, $0.04 on the 3rd day, etc.). Ask the user to enter the number of days she will work (you may assume up to 20 days only). For each day, show the day number, pay for the day, and accumulated total pay, with the numbers right-aligned in their columns (so the decimal points line up). No commas required in the numbers. No validation required. Only needs to work once before going back to the menu. (Warning -– check Day 20 carefully.)
Enter number of days: 4
DAY PAY TOTAL PAY
1 0.01 0.01
2 0.02 0.03
3 0.04 0.07
4 0.08 0.15
Option C: Ask the user to enter a letter. Using nested for loops, display the following triangular pattern, starting from the letter entered and ending with A. Each letter must be printed individually. Do not use arrays or strings, only individual characters. No validation required. Convert a small letter to uppercase before starting. Only needs to work once before going back to the menu. Examples:
Enter a letter: F
F
FE
FED
FEDC
FEDCB
FEDCBA
Enter a letter: D
D
DC
DCB
DCBA
Program hierarchy:
MAIN FUNCTION
-contains main loop
(continues until X chosen)
-calls display menu function
-switch calls required function
DISPLAY MENU
-displays menu
-inputs/returns choice
SCAFFOLD PAY CALCULATION TRIANGLE PATTERN
-contains y/n loop -inputs no. of days -inputs user’s letter
-inputs user’s number -calcs & prints results -displays pattern
-calls calculation function (using a loop) (using nested fors) (passing user’s number)
-displays result
(returned by calc func)
-prompts user to repeat (y/n)
ODD-NUMBER SUM CALC
-contains for loop
-calculates sum
(using number passed)
-returns result
Warning: No global variables permitted –- variables must be passed to and returned by the functions.
|
|
|
10-22-2006, 08:23 AM
|
#2 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
you said you have the menu function figured out, post your code and we'll go from there.
Your option A shouldn't be that difficult to write....something like this may work:
Code:
#include <iostream>
using namespace std;
void main()
{
int i = 1;
int total = 1;
int userinput;
cout <<"Enter a positive whole number";
cin >> userinput;
do
{
i += 2;
if(i > userinput)
break;
else
total += i;
}while(i < userinput);
cout <<"Total sum of numbers between 1 and " << userinput << " is " << total << endl;
}
Then, it's a matter of writing this into 2 functions one of which will actually do the calculation, the other will be passed the return value of your calculation to display with. both these functions will be invoked from your menu which you have established....
for your last option (the pyramid structure) you should be able to accomplish this with two loops (look around the board, there was an example of something that did that (outside loop keeps track of rows, inside loop of columns)
|
|
|
10-22-2006, 08:28 AM
|
#3 (permalink)
|
|
krisl100
Join Date: Oct 2006
Location: Montreal
Posts: 34
|
Ok, here is my code:
Code:
#include <stdafx.h>
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int menu();
char display_menu();
int main()
{
menu();
cin.get();
return 0;
}
int menu()
{
char choice;
do
{
choice = toupper(display_menu());
switch (choice)
{
case 'A':
cout << "Please enter a positive whole number: " << endl;
break;
case 'B':
cout << "Please enter the number of days you will work: " << endl;
break;
case 'C':
cout << "Enter a letter: " << endl;
break;
case 'X':
break;
default:
cout << endl << "\a Invalid choice -- try again" << endl;
}
} while (choice != 'X');
return 0;
}
char display_menu()
{
char choice;
cout << endl << endl;
cout << right << setw(10) << "MAIN MENU" << endl;
cout << "A. Please enter a positive whole number: " << endl;
cout << "B. Please enter the number of days you will work: " << endl;
cout << "C. Enter a letter: " << endl;
cout << "X. Exit" << endl;
cout << "Make a choice: ";
cin >> choice;
return choice;
}
|
|
|
10-22-2006, 08:31 AM
|
#4 (permalink)
|
|
krisl100
Join Date: Oct 2006
Location: Montreal
Posts: 34
|
The code you wrote, how do I set it so that it calls the menu function, so that when I press A, it runs the program for calculating the whole number?
The code example you gave, is that a seperate function as well?
Thanks.
|
|
|
10-22-2006, 08:36 AM
|
#5 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
if you'd like to implement a simple error-check to ensure your program only spits out valid data, try something like this after your cin:
Code:
while (cin.fail() || userinput < 1) //must be at least 1
{
cin.clear();
cin.ignore(1000,'\n');
cout << " Invalid input. Number must be a positive whole number:";
cin >> userinput;
}
when you enter incorrect input, the stream will break and return false.
cin.clear() will reset those flags
now, to flush out any data that is contained in the stream we use
cin.ignore(n,delimiter);
basically it will ignore up to 'n' characters, or until it reached the 'delimiter' which we specified as the newline character \n.
|
|
|
10-22-2006, 09:53 AM
|
#6 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Think of the switch case as a very long if/else/if/else/if/else/if/else/if/else block of statements. only one will ever be executed as your menuchoice variable is only going to hold 1 char. Any block of code you want executed by that case should be written right there.
case A should read something like:
Code:
case 'A':
total = optionA();
DisplayTotal(total);
break;
and respectively for all other functions. let's go through it quickly line by line:
The return value of optionA must be stored somewhere otherwise all your data will be lost once the function goes out of scope (like that previous example with the grades you were doing).
Code:
DisplayTotal(total);
This time, we want to create another function, a very trivial one which will output your result to the screen. We pass to it the value of total in its parameter list as we want our DisplayTotal function to know what value it is displaying...Depending on the format you may want to change the parameter list of the function to include the number entered by the user also so you can write something like "Total between 1 and X is Y" where X and Y are of type int.
the is simply letting the case know "nothing left to process" and will return execution back to the program.
|
|
|
10-25-2006, 09:56 PM
|
#7 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 2
|
take A for example:
case 'A':
cout << "Please enter a positive whole number: " << endl;
total = optionA();
break;
.
.
.
.
int optionA()
{
int i = 1;
int total = 1;
int userinput;
cout <<"Enter a positive whole number";
cin >> userinput;
do
{
i += 2;
if(i > userinput)
break;
else
total += i;
}while(i < userinput);
cout <<"Total sum of numbers between 1 and " << userinput << " is " << total << endl;
return total;
}
|
|
|
10-26-2006, 05:22 AM
|
#8 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 2
|
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
using std::string;
using namespace std;
int menu();
char display_menu();
char optionA();
char optionB();
char optionC();
char choice;
int main()
{
menu();
cin.get();
return 0;
}
int menu()
{
choice = toupper(display_menu());
do
{
switch (choice)
{
case 'A':
choice = optionA();
break;
case 'B':
choice = optionB();
break;
case 'C':
choice = optionC();
break;
case 'X':
break;
default:
cout << endl << "\a Invalid choice -- try again" << endl;
}
} while (choice != 'X');
return 0;
}
char optionA()
{
string name;
cout<<"Please enter your name: "<<endl;
cin>>name;
cout<<"Hello!"<<name<<"your telephone number is 13809534502"<<endl;
cout<<"Make another choice: "<<endl;
cin>>choice;
return choice;
}
char optionB()
{
string name;
cout<<"Please enter your name: "<<endl;
cin>>name;
cout<<"Hello!"<<name<<"your telephone number is 5425689"<<endl;
cout<<"Make another choice: "<<endl;
cin>>choice;
return choice; }
char optionC()
{
string name;
cout<<"Please enter your name: "<<endl;
cin>>name;
cout<<"Hello!"<<name<<"your telephone number is 5555555"<<endl;
cout<<"Make another choice: "<<endl;
cin>>choice;
return choice; }
char display_menu()
{
cout << endl << endl;
cout << right << setw(10) << "MAIN MENU" << endl;
cout << "If you learn GIS,please enter A: " << endl;
cout << "If you learn human,please enter B: " << endl;
cout << "If you learn dia,please enter C: " << endl;
cout << "If you want to EXIT,please enter X: " << endl;
cout << "Make a choice: ";
cin >> choice;
return choice;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:21 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|