| View previous topic :: View next topic |
| Author |
Message |
Mapleblitzer Master Cheater
Reputation: 0
Joined: 08 Apr 2007 Posts: 254
|
Posted: Sat Aug 25, 2007 6:46 pm Post subject: Go back to list after finish? |
|
|
Hi,
I have a script here in C++ and I want it so that after it finishes a function, it goes back to the selection menu. I just started c++ yesterday, so please don't get angry at me if I don't quite understand a lot of what you say.
| Code: | //First program made by Chris :O
#include <iostream>
using namespace std;
int loop;
int result;
int num1;
int num2;
int selection;
int i;
// this function is for adding
void add(int num1) {
for(int i = 0; i < loop; ++i) { //Enters each number specified by loop
//and adds it to result each time the loop goes around
cout << "Please enter number " << i << " of" << loop << "\n";
cin >> num1;
result = result + num1;
}
cout << "The result of adding your numbers is " << result << "\n";
}
// this function is for subtracting
void sub(int num1) {
for (i = 0; i<loop; ++i) { // Loops each time until i is equal to loop
cout << "Please enter number " << i << " of" << loop << "\n";
cin >> num1;
result = num1 - result;
}
cout << "The result of adding your numbers is " << result << "\n";
}
//function for dividing
void divide(int num1) { //by now I think you know what this does, if not see
//comments above
result = 1; // set result to 1, so that you divide by 1 instead of 0 the
//first time
for (i = 0; i<loop; ++i) {
cout << "Please enter number " << i << " of" << loop << "\n";
cin >> num1;
result = num1 / result;
}
cout << "The result of dividing your numbers is " << result << "\n";
}
void Multiply(int num1) {
result = 1;
for (i = 0; i<loop; ++i) {
cout << "Please enter number " << i << " of" << loop << "\n";
cin >> num1;
result = result * num1;
}
cout << "The result of multiplying your numbers is " << result << "\n";
}
int main() {
cout << "Welcome to Chris' calculator v1.0!\n";
cout << "Please enter the number of numbers you will be using \n";
cin >> loop; //receives the number of numbers that it will deal with
cout << "Thank you for entering the numbers. Please select from the list shown (enter the number only please):\n";
//for the switch command, the 5 choices
cout << "1. Add \n" << "2. Subtract \n" << "3. Divide \n" << "4. Multiply \n" << "5. Leave \n";
//receive the selection
cin >> selection;
//respond accordingly to what selection is
switch (selection) {
case 1:
add(loop);
break;
case 2:
sub(loop);
break;
case 3:
divide(loop);
break;
case 4:
Multiply(loop);
break;
case 5:
break; //exit
}
cout << "Please press enter to exit. \n";
cin.ignore(); //clears the input so that the user can press enter
cin.get(); //gets enter to exit
} |
Any help is appreciated.
|
|
| Back to top |
|
 |
zart Master Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 351 Location: russia
|
Posted: Sat Aug 25, 2007 7:08 pm Post subject: |
|
|
Just quickly glancing at it, I'll let someone else comment on the usage of your code (kinda ugly - though it works - and I don't feel like writing up a full page on it)
As simple way to keep it looping is to drop the whole "menu part" into a do-while loop. Have the 'break' case set a flag. When it hits the end of the loop it will jump out of the loop since the flag was tripped.
Also - your using "int main" so return something at the end. I'm sure others will comment much more thoroughly.
_________________
0x7A 0x61 0x72 0x74
TEAM RESURRECTiON |
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sat Aug 25, 2007 7:22 pm Post subject: |
|
|
| Use comments to explain why, not what or how
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sat Aug 25, 2007 8:20 pm Post subject: |
|
|
do a while loop so that when they type in 0 or quit it breaks the loop and closes
like 0 for quit/1 for repeat
_________________
|
|
| Back to top |
|
 |
Mapleblitzer Master Cheater
Reputation: 0
Joined: 08 Apr 2007 Posts: 254
|
Posted: Mon Aug 27, 2007 8:40 pm Post subject: |
|
|
Hi,
Sorry I didn't reply sooner, I've kinda been stuck on some topics in C++ and have been trying to sort them out.
Here's my newly updated code:
| Code: | #include <iostream>
using namespace std;
//The variables
int result;
int num1;
int num2;
int i;
int x;
int exit2;
int selection;
int loop;
inline void add(int num1) {
for(int i = 0; i < loop; ++i) { //To calculate multiple numbers
cout << "\nPlease enter number " << i << " of" << loop << "\n\n";
cin >> num1;
result = result + num1;
}
cout << "The result of adding your numbers is " << result << "\n\n";
}
inline void sub(int num1) {
for (i = 0; i<loop; ++i) { //To calculate multiple numbers
cout << "\nPlease enter number " << i << " of" << loop << "\n\n";
cin >> num1;
result = num1 - result;
}
cout << "The result of adding your numbers is " << result << "\n\n";
}
inline void divide(int num1) { //To calculate multiple numbers
result = 1; //set result = 1 because you can't divide by 0
for (i = 0; i<loop; ++i) {
cout << "\nPlease enter number " << i << " of" << loop << "\n\n";
cin >> num1;
result = num1 / result;
}
cout << "The result of dividing your numbers is " << result << "\n\n";
}
inline void Multiply(int num1) {
result = 1; //set result = 1 because multiplying by 0 will give you the wrong answer
for (i = 0; i<loop; ++i) {
cout << "\nPlease enter number " << i << " of" << loop << "\n\n";
cin >> num1;
result = result * num1;
}
cout << "The result of multiplying your numbers is " << result << "\n\n";
}
int main () {
cout << "Welcome to Chris' calculator v1.0!\n";
do { //do..while command so that the user can use each function again without restarting the program
//Thank you zart
cout << "\nPlease enter the number of numbers you will be using (Enter 0 to exit): \n\n";
cin >> loop; //receives the number of numbers that it will deal with
if (loop==0) {
break;
}
cout << "\nThank you for entering the numbers. Please select from the list shown (enter the number only please):\n\n";
cout << "1. Add \n" << "2. Subtract \n" << "3. Divide \n" << "4. Multiply \n" << "5. Leave \n\n";
cin >> selection;
switch (selection) {
case 1:
add(loop);
break;
case 2:
sub(loop);
break;
case 3:
divide(loop);
break;
case 4:
Multiply(loop);
break;
case 5:
x = 1; //For the "while" part, in order to exit
continue;
}
}
while (x == 0);
return 0;
} |
I was wondering how I would make a class with the functions as private and the variables as public. I tried this:
| Code: | #include <iostream>
using namespace std;
//The variables
class variables {
private:
int result;
int num1;
int num2;
int i;
int x;
int exit2;
int selection;
int loop;
public:
inline void add(int num1) {
for(int i = 0; i < loop; ++i) { //To calculate multiple numbers
cout << "\nPlease enter number " << i << " of" << loop << "\n\n";
cin >> num1;
result = result + num1;
}
cout << "The result of adding your numbers is " << result << "\n\n";
}
inline void sub(int num1) {
for (i = 0; i<loop; ++i) { //To calculate multiple numbers
cout << "\nPlease enter number " << i << " of" << loop << "\n\n";
cin >> num1;
result = num1 - result;
}
cout << "The result of adding your numbers is " << result << "\n\n";
}
inline void divide(int num1) { //To calculate multiple numbers
result = 1; //set result = 1 because you can't divide by 0
for (i = 0; i<loop; ++i) {
cout << "\nPlease enter number " << i << " of" << loop << "\n\n";
cin >> num1;
result = num1 / result;
}
cout << "The result of dividing your numbers is " << result << "\n\n";
}
inline void Multiply(int num1) {
result = 1; //set result = 1 because multiplying by 0 will give you the wrong answer
for (i = 0; i<loop; ++i) {
cout << "\nPlease enter number " << i << " of" << loop << "\n\n";
cin >> num1;
result = result * num1;
}
cout << "The result of multiplying your numbers is " << result << "\n\n";
};
int main () {
cout << "Welcome to Chris' calculator v1.0!\n";
do { //do..while command so that the user can use each function again without restarting the program
//Thank you zart
cout << "\nPlease enter the number of numbers you will be using (Enter 0 to exit): \n\n";
cin >> loop; //receives the number of numbers that it will deal with
if (loop==0) {
break;
}
cout << "\nThank you for entering the numbers. Please select from the list shown (enter the number only please):\n\n";
cout << "1. Add \n" << "2. Subtract \n" << "3. Divide \n" << "4. Multiply \n" << "5. Leave \n\n";
cin >> selection;
switch (selection) {
case 1:
variables::add(loop);
break;
case 2:
variables::sub(loop);
break;
case 3:
variables::divide(loop);
break;
case 4:
variables:Multiply(loop);
break;
case 5:
x = 1; //For the "while" part, in order to exit
continue;
}
}
while (x == 0);
return 0;
} |
However, when I do this, at the end it says "expected "}" at end of input, but I already have one after the "while" line.
Thanks,
Mapleblitzer.
Edit: Also, how can I make my code less "ugly?" Do you mean make the script shorter?
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Tue Aug 28, 2007 2:49 am Post subject: |
|
|
You can make the code less ugly by indenting which is what i have trouble with and was told to go here for http://en.wikipedia.org/wiki/Indent_style.
Also for me there are things that make it easyer to read.
Like this
| Code: | cout<<"Hello"
<<"this is a example"
<<"of how i like to write my couts"<<endl;
|
But i am a worse programer than you since i only picked up a little programing.
yet again thanks to anyone who has helped me and who ever gave me that link.
Oh yeah i forgot when delcaring variables you can do this.
| Code: | | int a, b, c , d, and, so, on; |
Edit: Im adding my coded verision that is much bigger to it.
A full 108 lines it took me to get it done this probly wont help though.
| Code: | #include <iostream>
int multiply(int loop);
int add(int loop); // here are my prototypes
int subtract(int loop);
int devide(int loop);
int main()
{
int x=0;
do
{
int control,loop;
std::cout << "How many numbers will you be using (Enter 0 to exit): ";
std::cin >> loop; //receives the number of numbers that it will deal with
if (loop==0) break;
std::cout<<"Enter 1 to add, 2 to subtract, 3 to multiply" //explaining rules
<<"and 4 to devide and 5 to exit: ";
std::cin >> control;
switch(control) // switch to call the functions
{
case 1:
add(loop);
break;
case 2:
subtract(loop);
break;
case 3:
multiply(loop);
break;
case 4:
devide(loop);
break;
case 5:
x=1;
break;
}
if(control==5) break;
} while(x==0);
}
int add(int loop) //This function will be copyed then pasted and modified for all the functions
{
int result=0,add; //setting up basic variables
for(int control=0;;loop--)
{
std::cout<<"What number would you like to add: ";
std::cin >> add;
result=add+result;
std::cout<<"You have "<<loop
<<" Numbers left to add"<<std::endl;
if (loop==control)
{
std::cout << "Result is " << result << std::endl;
break;
}
}
}
int subtract(int loop) //Same as the first 1 with 1 diffrent variable and doing subtraction
{
int result=0,subtract;
for(int control=0;;loop--)
{
std::cout<<"What number would you like to subtract: ";
std::cin >> subtract;
result=subtract-result;
std::cout<<"You have "<<loop
<<" Numbers left to subtract"<<std::endl;
if (loop==control)
{
std::cout << "Result is " << result << std::endl;
break;
}
}
}
int multiply(int loop)
{
int result=1,multiply; //If you make result = 0 every multiplacation after will be 0
for(int control=0;;loop--)
{
std::cout<<"What number would you like to multiply: ";
std::cin >>multiply;
result=multiply*result;
std::cout<<"You have "<<loop
<<" Numbers left to add"<<std::endl;
if (loop==control)
{
std::cout << "Result is " << result << std::endl;
break;
}
}
}
int devide(int loop)
{
double result=1,devide; //If you make result = 0 you cant devide by 0; and double so you can get a decimal.
for(int control=0;;loop--)
{
std::cout<<"What number would you like to devide: ";
std::cin >> devide;
result=devide/result;
std::cout<<"You have "<<loop
<<" Numbers left to add"<<std::endl;
if (loop==control)
{
std::cout << "Result is " << result << std::endl;
break;
}
}
} |
There might be some extra lines in there or stuff unused since im half asleep and dead tired while doing this.
That is how i find it easyer to read and also i bet any experience programer here wills ay my indenting / coding method in general on this sucks. You probly cant learn anything from it but i offered my method. Which is from a noob with about as much exprience as you.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Tue Aug 28, 2007 9:10 am Post subject: |
|
|
| Mapleblitzer wrote: | | However, when I do this, at the end it says "expected "}" at end of input, but I already have one after the "while" line. |
class's must end with a not just a }
to make it less ugly, use more advanced syntax and make it alot neater
_________________
|
|
| Back to top |
|
 |
Mapleblitzer Master Cheater
Reputation: 0
Joined: 08 Apr 2007 Posts: 254
|
Posted: Tue Aug 28, 2007 10:07 am Post subject: |
|
|
| lurc wrote: | | Mapleblitzer wrote: | | However, when I do this, at the end it says "expected "}" at end of input, but I already have one after the "while" line. |
class's must end with a not just a }
to make it less ugly, use more advanced syntax and make it alot neater |
The }; was a mistake on my part, I thought I had added it, but I actually just added the ";" part to the closing bracket of a different function. I did what you said, but then in the switch (selection) list, it gives me an error saying that the add function is undeclared.
Also, could you give me an example of some more advanced syntax that would shorten this?
Thanks,
Mapleblitzer.
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Tue Aug 28, 2007 10:30 am Post subject: |
|
|
Because Add() etc. are members of the variables class (quick note: class names should be Capitalized, instances of a class should be lowercase.), you need to access them using
ClassName::function()
or
instanceofclass.function().
So,
or
| Code: | variables vinstance = new variables();
vinstance.Add(5); |
~nog_lorp
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
Mapleblitzer Master Cheater
Reputation: 0
Joined: 08 Apr 2007 Posts: 254
|
Posted: Tue Aug 28, 2007 11:06 am Post subject: |
|
|
| I did that before too (the variables:: part), but it says that I don't have an object defined inbetween the brackets in add(), even though I have loop in there.
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Sat Sep 01, 2007 7:29 am Post subject: |
|
|
| Mapleblitzer wrote: | | I did that before too (the variables:: part), but it says that I don't have an object defined inbetween the brackets in add(), even though I have loop in there. |
Ok here is a source of what i think you whanted. It does almost everything in the class with only a few variables and one function that are not private.
Nvm error in that source i fix it and put back up.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Sat Sep 01, 2007 7:55 am Post subject: Re: Go back to list after finish? |
|
|
| Code: | //First program made by Chris :O
#include <iostream>
using namespace std;
int loop;
int result;
int num1;
int num2;
int selection;
int i;
// this function is for adding
void add(int num1) {
for(int i = 0; i < loop; ++i) { //Enters each number specified by loop
//and adds it to result each time the loop goes around
cout << "Please enter number " << i << " of" << loop << "\n";
cin >> num1;
result = result + num1;
}
cout << "The result of adding your numbers is " << result << "\n";
}
// this function is for subtracting
void sub(int num1) {
for (i = 0; i<loop; ++i) { // Loops each time until i is equal to loop
cout << "Please enter number " << i << " of" << loop << "\n";
cin >> num1;
result = num1 - result;
}
cout << "The result of adding your numbers is " << result << "\n";
}
//function for dividing
void divide(int num1) { //by now I think you know what this does, if not see
//comments above
result = 1; // set result to 1, so that you divide by 1 instead of 0 the
//first time
for (i = 0; i<loop; ++i) {
cout << "Please enter number " << i << " of" << loop << "\n";
cin >> num1;
result = num1 / result;
}
cout << "The result of dividing your numbers is " << result << "\n";
}
void Multiply(int num1) {
result = 1;
for (i = 0; i<loop; ++i) {
cout << "Please enter number " << i << " of" << loop << "\n";
cin >> num1;
result = result * num1;
}
cout << "The result of multiplying your numbers is " << result << "\n";
}
int main() {
cout << "Welcome to Chris' calculator v1.0!\n";
while (TRUE)
{
cout << "Please enter the number of numbers you will be using \n";
cin >> loop; //receives the number of numbers that it will deal with
cout << "Thank you for entering the numbers. Please select from the list shown (enter the number only please):\n";
//for the switch command, the 5 choices
cout << "1. Add \n" << "2. Subtract \n" << "3. Divide \n" << "4. Multiply \n" << "5. Leave \n";
//receive the selection
cin >> selection;
//respond accordingly to what selection is
switch (selection) {
case 1:
add(loop);
break;
case 2:
sub(loop);
break;
case 3:
divide(loop);
break;
case 4:
Multiply(loop);
break;
case 5:
cout << "Please press enter to exit. \n";
cin.ignore(); //clears the input so that the user can press enter
cin.get(); //gets enter to exit
break; //exit
}
}
} |
Loop...?
The exit function is fucked up..i'm sure you could fix it..
(Just change the loop to while (!quit), then just set quit to true for case 5.)
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Sep 01, 2007 9:01 am Post subject: |
|
|
smartz u forgot to return
add "return 0;" before the last } and it'l work
_________________
|
|
| Back to top |
|
 |
DeltaFlyer Grandmaster Cheater
Reputation: 0
Joined: 22 Jul 2006 Posts: 666
|
Posted: Sat Sep 01, 2007 9:13 am Post subject: Re: Go back to list after finish? |
|
|
| smartz993 wrote: | Loop...?
The exit function is fucked up..i'm sure you could fix it..
(Just change the loop to while (!quit), then just set quit to true for case 5.) |
To exist simply change the loop to a do-while, and the condition to (selection != 5)...
The program has too many globals. Many are not even used or declared in duplicates as a local variable. Instead of declaring all the variables global, pass them in as parameters or just simply make them local to each function.
The functions are not well defined. Instead of putting all of the input/output in each function, where many things are simply duplicates with a change in one word (adding, subtracting, etc), put the IO stuff in the main function and leave the calculations to the operation functions. A function array could serve well here.
_________________
Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for? |
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Sat Sep 01, 2007 2:47 pm Post subject: |
|
|
Yawns ok here it is completely working with all functions private execept 1 function that is public to access them.
| Code: | #include <iostream>
int bigQuit;
class var
{
private:
void mul(int); // prototyping my private functions.
void dev(int);
void add(int);
void sub(int);
int result,num,num2;
double dnum,dnum2;
public:
int loop, quit,q;
void man(int); // the function that will access everything
};
var math;
void var::mul(int num)
{
num=num-1;
result=1;//setting it up so that result can actually be multiplied without always coming out 0
for(int loop=0;;num--) //Setting up a infinite loop that decreases num by 1 each pass
{
std::cout << "Enter number: ";
std::cin >> num2;
result=num2*result;
if(num==loop)break;
}
std::cout << "Number: "<< result << std::endl;
}
void var::dev(int num)
{
num=num-1;
result=1;//setting it up so that result can be devided
for(int loop=0;;num--) //Setting up a infinite loop that decreases num by 1 each pass
{
std::cout << "Enter number: ";
std::cin >> num2;
dnum2=dnum/dnum2; // must use double for devision since decimals are very common
if(num==loop)break;
}
std::cout << "Number: " << dnum2 << std::endl;
}
void var::add(int num)
{
num=num-1;
result=0;
for(int loop=0;;num--) //Setting up a infinite loop that decreases num by 1 each pass
{
std::cout << "Enter number: ";
std::cin >> num2;
result=num2+result;
if(num==loop)break;
}
std::cout << "Number: "<< result << std::endl;
}
void var::sub(int num)
{
num=num-1;
result=0;//setting it up so that result can actually be multiplied without always coming out 0
for(int loop=0;;num--) //Setting up a infinite loop that decreases num by 1 each pass
{
std::cout << "Enter number: ";
std::cin >> num2;
result=num2*result;
if(num==loop)break;
}
std::cout << "Number: "<< result << std::endl;
}
void var::man(int a)
{
do
{
std::cout << " Enter 0 to quit, 1 to multiply, 2 to devide, 3 to add, and 4 to subtract: ";
std::cin >> quit;
switch(quit)
{
case 0:
bigQuit=1;
break;
case 1:
var::mul(a);
break;
case 2:
var::dev(a);
break;
case 3:
var::add(a);
break;
case 4:
var::sub(a);
break;
}
} while(q=0);
}
int main()
{
int c;
std::cout << "Enter 0 to quit. \nHow many numbers are you using: ";
std::cin >> c;
if(c==0) return EXIT_SUCCESS;
for(;;)
{
math.man(c);
if(bigQuit==1)break;
}
} |
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
|