Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-22-2006, 03:33 AM   #1 (permalink)
krisl100
krisl100
 
Join Date: Oct 2006
Location: Montreal
Posts: 34
krisl100 is on a distinguished road
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.
krisl100 is offline   Reply With Quote
Old 10-22-2006, 08:23 AM   #2 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
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)
Deliverance is offline   Reply With Quote
Old 10-22-2006, 08:28 AM   #3 (permalink)
krisl100
krisl100
 
Join Date: Oct 2006
Location: Montreal
Posts: 34
krisl100 is on a distinguished road
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;

}
krisl100 is offline   Reply With Quote
Old 10-22-2006, 08:31 AM   #4 (permalink)
krisl100
krisl100
 
Join Date: Oct 2006
Location: Montreal
Posts: 34
krisl100 is on a distinguished road
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.
krisl100 is offline   Reply With Quote
Old 10-22-2006, 08:36 AM   #5 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
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.
Deliverance is offline   Reply With Quote
Old 10-22-2006, 09:53 AM   #6 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
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:
Code:
total = optionA();
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
Code:
 break;
is simply letting the case know "nothing left to process" and will return execution back to the program.
Deliverance is offline   Reply With Quote
Old 10-25-2006, 09:56 PM   #7 (permalink)
jackshen016
Recruit
 
Join Date: Oct 2006
Posts: 2
jackshen016 is on a distinguished road
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;
}
jackshen016 is offline   Reply With Quote
Old 10-26-2006, 05:22 AM   #8 (permalink)
jackshen016
Recruit
 
Join Date: Oct 2006
Posts: 2
jackshen016 is on a distinguished road
#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;

}
jackshen016 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 08:51 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting