| | |
Loop/Function errors
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 1
Hi there, been working on a small program that will loop a function which adds two integers until the user enters 00, but can't seem to work out the finer details. A little newer to C++, don't need a complete answer but any advice would be much appreciated
And i'm pulling in these errors;
documents\visual studio 2008\projects\functions\functions\source.cpp(19) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(26) : error C2144: syntax error : 'int' should be preceded by ')'
c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(26) : error C2059: syntax error : ')'
c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(28) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
c++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main () { double x, y ; cout << "Welcome to the Functions Program! To quit enter 00." << endl ; cout << "Input two numbers." << endl ; cin >> x, y ; while (x || y != 00) ; { int z ; z = (x,y); cout << "The result is " << z; return 0 ; } int (int x, int y) ; { int r; r=x+y; return (r); } }
And i'm pulling in these errors;
documents\visual studio 2008\projects\functions\functions\source.cpp(19) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(26) : error C2144: syntax error : 'int' should be preceded by ')'
c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(26) : error C2059: syntax error : ')'
c:\documents and settings\owner\my documents\visual studio 2008\projects\functions\functions\source.cpp(28) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
> while (x || y != 00) ;
1. Remove the ; at the end
2. This isn't basic. If you want to compare both with 0, then do
while (x != 0 || y != 0)
> z = (x,y);
Meaning what?
z = y;
is what you've written.
z is int, y is double, so that's where the first problem is.
> int (int x, int y) ;
Go back to your book and study how functions are declared.
1. Remove the ; at the end
2. This isn't basic. If you want to compare both with 0, then do
while (x != 0 || y != 0)
> z = (x,y);
Meaning what?
z = y;
is what you've written.
z is int, y is double, so that's where the first problem is.
> int (int x, int y) ;
Go back to your book and study how functions are declared.
C++ Syntax (Toggle Plain Text)
while (x || y != 00) ;
while ( x OR y ) not equal to 0 which compares the boolean result of ORing x and y, comparing that result to 0. To do what you want (continue while neither x nor y is a 0) try C++ Syntax (Toggle Plain Text)
while ( x != 0 && y != 0 ) { //do stuff here }
Also note that the semicolon following your while statement becomes the action it execute - an empty statement.
Your adding function needs a name, and you need to use that name where you execute the function (line 19 )
And, you need to either place the adding function before main( ), or put a prototype for the function before main( ). The compiler needs to see the function before you can use it.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
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.
Here..this is a function..
c++ Syntax (Toggle Plain Text)
int f_i(int x,int y) { //... return (x+y); } // you meant // z = f_i(...,...)!!
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 1
Alright, i appreciate the advice here. I do need to get into the books more, even with reading i've had some trouble understanding this though and trail and error seems to be the only way to figure it out. I dont have a compiler handy since im at work, but does this more like what i was going for?
c++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int add(int x, int y) int main () { int x, y ; cout << "Welcome to the Functions Program! To quit enter 00." << endl ; cout << "Input two numbers." << endl ; cin >> x, y ; while (x != 00 || y != 00) { int z ; z = add(x,y); cout << "The result is " << z; return 0 ; } int add(int x, int y) { return (x+y); } }
•
•
Join Date: Mar 2008
Posts: 1,495
Reputation:
Solved Threads: 123
When things get this bad, honestly, just start over.
http://www.cplusplus.com/doc/tutorial/
Understand how to use functions and make simple expressions, also, keep your code well formatted. Here is how your current code would look well formatted, but all the mistakes are still there. I have highlighted your mistakes, see if you can fix them.
http://www.cplusplus.com/doc/tutorial/
Understand how to use functions and make simple expressions, also, keep your code well formatted. Here is how your current code would look well formatted, but all the mistakes are still there.
#include<iostream> using namespace std; int add(int x, int y) // your missing something here.. int main () { int x, y; cout << "Welcome to the Functions Program! To quit enter 00." << endl; cout << "Input two numbers." << endl; cin >> x, y; while (x != 00 || y != 00) // Padding with extra 0 changes the number base to octal, which I doubt you need here { int z; z = add(x,y); cout << "The result is " << z; return 0; // Whats the point in the while loop, if this is here } int add(int x, int y) { return (x+y); } }
Last edited by William Hemsworth; Nov 25th, 2008 at 2:39 pm.
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 1
Alright, this will be the last post until i can get to a compiler tonight. I believe i fixed most of what you mentioned, and i do appreciate the feedback. I know the last source post was kind of sloppy looking, at work and was paying attention to something else when i posted it =/
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 << endl ; } }
Last edited by cout<<"alias"; Nov 25th, 2008 at 3:52 pm.
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 1
Will the if statement allow the user to use the program over and over until they enter 0? This was my main objective with the program, that and incorporating functions. lol, and thank you its a rough road for getting the syntax and format perfect but im working on it. My only programming experience thus far besides Python.
![]() |
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
Views: 1109 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






(except for some of the formatting)