| | |
Loop/Function errors
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 1
Alright, that makes sense. Here is what i have so far, when i get home going to try and compile it and see what happens. Thanks for all the input so far guys, i appreciate your time on this.
c++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int add(int x, int y) { int r; r = x+y; return (r); } int main () { int x, y ; cout << "Welcome to the Functions Program! To quit enter 0." << endl ; cout << "Input two numbers." << endl ; cin >> x >> y ; while (x != 0 || y != 0) { int z ; z = add(x,y); cout << "The result is " << z; cout << "Would you like to continue? If so enter another two numbers, if not enter 0" << endl ; cin >> x >> y ; } }
Last edited by cout<<"alias"; Nov 25th, 2008 at 6:05 pm.
•
•
Join Date: Mar 2008
Posts: 1,418
Reputation:
Solved Threads: 114
Woo! you did it without anybody writing the code for you.. (it's a nice change)
but, here are just a few final touches to it.. along with formatting it for you (for the second time)
but, here are just a few final touches to it.. along with formatting it for you (for the second time)
#include <iostream>
using namespace std;
int add(int x, int y) {
int r;
r = x + y;
return(r);
}
int main () {
int x, y;
cout << "Welcome to the Functions Program! To quit enter 0." << endl;
cout << "Input two numbers." << endl;
cin >> x >> y;
while (x != 0 || y != 0) {
int z = add(x, y);
cout << "The result is " << z;
cout << "\nWould you like to continue? If so enter another two numbers, if not enter 0" << endl;
cin >> x >> y ;
}
} Last edited by William Hemsworth; Nov 25th, 2008 at 6:42 pm.
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 1
Alright, the loop/function complied correctly thanks to everyones help. I wanted to see if anyone might have some ideas/feeback on this idea though. The original program was simply a while loop that called a function to add two integers. I tried to modify it so that there was a function for addition/subtraction/multiplication/division and the user could choose with operation they would like to perform by inputing a third operator that would call one of those functions. My question was would a while loop still suffice for this, or should i use other conditionals like 'if'? I couldnt get the loop to call any other function besides addition. Ill go ahead and post the first half the code so you can see what i have so far, any ideas are welcome.
Trying to decide where to go from here...
c++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int add(int x, int y) { int r ; r = x+y ; return (r) ; } int sub(int x, int y) { int w ; w = x-y; return (w) ; } int mult(int x, int y) { int b; b = x*y ; return (b) ; } int div(int x, int y) { int q; q = x/y; return (q); } int main() { int x,y,z; cout << "Welcome to the Functions program! To exit at anytime enter 0." << endl ; cout << "Input two numbers." << endl; cin >> x >> y ; cout << "Now enter 1 to add, 2 to subtract, 3 to multipy, 4 to divide" << endl ; cin >> z ;
Trying to decide where to go from here...
Last edited by cout<<"alias"; Nov 26th, 2008 at 12:05 pm.
•
•
Join Date: Mar 2008
Posts: 1,418
Reputation:
Solved Threads: 114
Use a switch statement, it should look along the lines of.. You will have to understand and add most of the functionality yourself 
For more help on this topic.
C++ Syntax (Toggle Plain Text)
switch ( z ) { case 1: // Add { } break; case 2: // Subtract { } break; case 3: // Multiply { } break; case 4: // Divide { } break; }

For more help on this topic.
Last edited by William Hemsworth; Nov 26th, 2008 at 12:23 pm.
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 1
Alright this is what i put together
It gets hung up on the while loop though, trying to figure out how to loop it but no luck so far.
c++ Syntax (Toggle Plain Text)
switch ( z ) { case 1: //add sum = add(x,y); cout << "Your sum is: " << sum << endl; break; case 2: // Subtract sum = sub(x,y); cout << "Your sum is: " << sum << endl; break; case 3: // Multiply sum = mult(x,y); cout << "Your sum is: " << sum << endl; break; case 4: // Divide sum = divi(x,y); cout << "Your sum is: " << sum << endl; break; } while (x != 0 || y != 0) { cout << "Would you like to continue? If so enter two more numbers." << endl; cout << "Otherwise enter 0's." << endl ; cin >> x >> y ; cout << "Which Operation?" << endl ; cin >> z ; } }
It gets hung up on the while loop though, trying to figure out how to loop it but no luck so far.
Last edited by cout<<"alias"; Nov 26th, 2008 at 4:09 pm.
Your while loop says "Stay here until the user enters a zero for both x and y. Even if I enter two zeroes, I still have to enter an operation option?
You really shouldn't have input values and stop or go value in the same block like that. For the kind of problem you're doing, a good structure might be something like:
You really shouldn't have input values and stop or go value in the same block like that. For the kind of problem you're doing, a good structure might be something like:
C++ Syntax (Toggle Plain Text)
do { get option ( 1,2,3,4 or 0 to quit) if( option not 0 ) { get inputs (x and y) switch( option ) { //cases 1-4 as you've done default: //bad input message } } }while ( option not 0 );
Last edited by vmanes; Nov 26th, 2008 at 4:56 pm.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 1
Yep, thats what did it right there. Didnt even think about using a 'do-while' loop. Appreciate that, was becoming a headache.
•
•
•
•
Your while loop says "Stay here until the user enters a zero for both x and y. Even if I enter two zeroes, I still have to enter an operation option?
You really shouldn't have input values and stop or go value in the same block like that. For the kind of problem you're doing, a good structure might be something like:
C++ Syntax (Toggle Plain Text)
do { get option ( 1,2,3,4 or 0 to quit) if( option not 0 ) { get inputs (x and y) switch( option ) { //cases 1-4 as you've done default: //bad input message } } }while ( option not 0 );
![]() |
Similar Threads
- Binary Recursion Function Errors - Help!! (C++)
- Bubble sort & File output jibrish errors??? (C)
- creating our own C++ strcat() function, code reqd (C++)
- please help with insertion sort (C++)
- help with functions (Python)
- Wierd error messages with calculator program (C++)
- Newbie needs help with mortgage program (C++)
- Keyboard input or "stuck in a loop" (C++)
- Many Errors while doing this assignment (C)
- Please Check The Errors'..Dont know what else to do (C++)
Other Threads in the C++ Forum
- Previous Thread: Help needed to use multimap
- Next Thread: Numeric Validation Query
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






