My coding (n00bish)

New Member
Joined
Jul 4, 2003
Messages
312
Best answers
0
maybe this should go inthe help section .........
i am a n00b to coding..........i started 2 days ago can anyone tell me wats wrong with this code......i am using C++ and a win32 console???...... there are 4 errors in this can anyone spot them....i cant


// EX1_01.CPP
// A Simple Program
#include <iostream>
using namespace std;

int main()
{
int apples, oranges, peaches, tomatos; // Declare 4 integer variables
int fruit; // ...then another one
int veges; // ... then another one


apples = 5; oranges = 6; peaches = 7; tomatos = 5; // Set initial values
fruit = apples + oranges + peaches; // Get the total fruit
veges = tomatos; // Get the total veges

cout << endl; // Start output on a new line
cout << "oranges are not the only fruit... " <<endl
<< "- and we have " << fruit << " fruits in all."<<endl

<< apples << " apples " << oranges << " oranges " << peaches << " peaches "endl
<< "BUT we will have " << apples + oranges + peaches + tomatos << " If we add veges "endl
<< "because we have " << tomatos << " tomatos ";
cout << endl; // Start output on a new line

return 0; // Exit the program
}

------------------------------------------------
thanx
 
New Member
💻 Oldtimer
Joined
May 14, 2003
Messages
1,929
Best answers
0
USSJ2vegeta said:
<< apples << " apples " << oranges << " oranges " << peaches << " peaches "endl
<< "BUT we will have " << apples + oranges + peaches + tomatos << " If we add veges "endl
Isn't there supposed to be something infront of endl?
 
New Member
💻 Oldtimer
Joined
Nov 22, 2002
Messages
2,347
Best answers
0
cout << "oranges are not the only fruit... " < << "- and we have " << fruit << " fruits in all."< << apples << " apples " << oranges << " oranges " < << "BUT we will have " << apples + oranges + peaches + tomatos << " If we add veges "endl
What errors are you getting (in detail)? That would help us alot. I'm no C++ expert but the:

Code:
<< " If we add veges "endl
should be:

Code:
<< " If we add veges " << endl;
This is assuming that the cout coming 3 lines up is techniqually on the same line in whatever program you are using. Maybe try something like this:

Code:
cout << "oranges are not the only fruit... ";
cout << "- and we have " << fruit << " fruits in all.";
cout << apples << " apples " << oranges << " oranges ";
cout << "But we will have " << apples + oranges + peaches + tomatos << " If we add veges" << endl;
That's what I'd do anyways, not too sure bout apples + oranges + peaches + tomatoes being used, cause you could just do:

Code:
int total;
total = apples + oranges + peaches + tomatos;
then exchange that long line there with total...

Code:
<< apples + oranges + peaches + tomatos
to

Code:
<< total
My 2 cents :rolleyes:
 
New Member
💻 Oldtimer
Joined
Mar 6, 2003
Messages
3,999
Best answers
0
Location
New York
im thinking this kid wants us to do his classwork for him :rolleyes:
 
New Member
Joined
Jul 4, 2003
Messages
312
Best answers
0
lol no..... i dont do programming at skool cause it doesnt have the subject and i am holidays now so........yes i edited some code and i came up with this........



// EX1_01.CPP
// A Simple Example of a Program
#include <iostream>
using namespace std;

int main()
{
int apples, oranges, peaches, tomatos; // Declare 4 integer variables
int fruit; // ...then another one
int veges; // ...then another one
int total; // ...then another one

apples = 5; oranges = 6; peaches = 7; tomatos = 5; // Set initial values
fruit = apples + oranges + peaches; // Get the total fruit
veges = tomatos; // Get the total veges
total = apples + oranges + peaches + tomatos // get the total total

cout << endl; // Start output on a new line
cout << "oranges are not the only fruit... " <<endl;
cout << "- and we have " << fruit << " fruits in all."<<endl;
cout << apples << " apples " << oranges << " oranges " << peaches << " peaches ";endl;

cout << "BUT we will have " << total << " If we add veges ";endl;
cout << "because we have " << tomatos << " tomatos ";
cout << endl; // Start output on a new line

return 0; // Exit the program
}



BUT i still get 1 error and 2 WARNINGS now they are

C:\Program Files\Microsoft Visual Studio\MyProjects\d\d.cpp(18) : error C2146: syntax error : missing ';' before identifier 'cout'

C:\Program Files\Microsoft Visual Studio\MyProjects\d\d.cpp(21) : warning C4551: function call missing argument list

C:\Program Files\Microsoft Visual Studio\MyProjects\d\d.cpp(22) : warning C4551: function call missing argument list

Error executing cl.exe.
 
Lost in space
Banned
💻 Oldtimer
Joined
Dec 1, 2002
Messages
2,725
Best answers
0
Well, I was actually so bored I copied your program and fixed it to compile it(In MSVC++ 6.0 atleast, I don't know what you're using for a compiler, but if nothing else, I needed the practice :D).
This should compile and run correctly.
Code:
// EX1_01.CPP
// A Simple Example of a Program
#include <iostream.h>

int main()
{
int apples, oranges, peaches, tomatos; // Declare 4 integer variables
int fruit; // ...then another one
int veges; // ...then another one
int total; // ...then another one

apples = 5; oranges = 6; peaches = 7; tomatos = 5; // Set initial values
fruit = apples + oranges + peaches; // Get the total fruit
veges = tomatos; // Get the total veges 
total = apples + oranges + peaches + tomatos; // get the total total

cout << endl; // Start output on a new line
cout << "oranges are not the only fruit... "; 
cout << "- and we have " << fruit << " fruits in all.";
cout << apples << " apples " << oranges << " oranges " << peaches << " peaches " << endl;

cout << "BUT we will have " << total << " If we add veges " << endl;
cout << "because we have " << tomatos << " tomatos ";
cout << endl; // Start output on a new line

return 0; // Exit the program
}
Only problems I found were probably just typos. Many instances of the insertion operator being typed as "<" rather than "<<", outputting cout and misplaced semicolons.
I didn't change the style, just the syntax. I'd suggest cleaning it up just a little though, to make it look nice. ;)
 
New Member
Joined
Jul 4, 2003
Messages
312
Best answers
0
WOW ok thanx this helps me out alot now i know what my program should have looked like and what i did ......
thanku
 
New Member
💻 Oldtimer
Joined
Nov 22, 2002
Messages
2,347
Best answers
0
We spoil you :). But once you actually make code that is like really long, this kinda help will be hard to find. Try to look up what your error is first. And are you learning from a book or a site?
 
New Member
Joined
Jul 4, 2003
Messages
312
Best answers
0
well i am just doing the tutorial that came with it ......... but i just wrote this program with a bit a help from it ...... and a lot of help from u guys...... but its completly different
 
Active Member
💻 Oldtimer
Joined
Aug 21, 2002
Messages
1,932
Best answers
0
USSJ2vegeta said:
#include <.iostream>
- #include <.iostream.h>
are you sure that you just need this library ??? (delete the dots)

USSJ2vegeta said:
using namespace std;
sorry ... dunno ... never heard about such a thing ... and never typed in my programs

USSJ2vegeta said:
int main()
{
int apples, oranges, peaches, tomatos; // Declare 4 integer variables
int fruit; // ...then another one
int veges; // ...then another one
int total; // ...then another one

apples = 5; oranges = 6; peaches = 7; tomatos = 5; // Set initial values
what's about:
int apples = 5, oranges = 6, peaches = 7, tomatos = 5;
instead of using 2 lines for it

USSJ2vegeta said:
fruit = apples + oranges + peaches; // Get the total fruit
veges = tomatos; // Get the total veges
total = apples + oranges + peaches + tomatos // get the total total
<--- C:\Program Files\Microsoft Visual Studio\MyProjects\d\d.cpp(18) : error C2146: syntax error : missing ';' before identifier 'cout'
total = apples + oranges + peaches + tomatos[size=+2];[/size]

USSJ2vegeta said:
cout << endl; // Start output on a new line
cout << "oranges are not the only fruit... " <<endl;
cout << "- and we have " << fruit << " fruits in all."<<endl;
cout << apples << " apples " << oranges << " oranges " << peaches << " peaches ";endl;
cout << "BUT we will have " << total << " If we add veges ";endl;
cout << "\noranges are not the only fruit... \n";
cout << "- and we have " << fruit << " fruits in all.\n";
cout << apples << " apples, " << oranges << " oranges and " << peaches << " peaches.\n"
cout << "BUT we would have " << total << ", if we add veges.\n";

you typed "endl;" this means it should execute the function "endl" ... it's not shown in a cout ... these are the warnings
I suggest to use "\n" instead of "endl" ... it's easier to include ... I hope you're able to use it after seeing it in my examples

USSJ2vegeta said:
cout << "because we have " << tomatos << " tomatos ";
cout << endl; // Start output on a new line
cout << "because we have " << tomatos << " tomatos.\n"
your code should work ... but here's another example for "\n" (new line)

USSJ2vegeta said:
return 0; // Exit the program
}
if you would use Borland compiler you would need a "getchar();" before the "return 0;" because (Borland is smarter than Microsoft :p and) there's no line to keep this window open ... Microsoft Compiler doesn't like this line ... but it keeps the window open til you tell it to close

tell me if it worked
 
New Member
Joined
Jul 4, 2003
Messages
312
Best answers
0
WOW information overload.............ur a really good coder ..........
i am using the compiler that comes with the program its faster 4 me anyways.........
i have a borland compiler and a TURBO c++ compiler aswell i just dont know how to use them yet.........
my compiler didnt like the ending part of ur code but.......it still is ok....

thanxing u
 
Lost in space
Banned
Joined
Oct 21, 2003
Messages
814
Best answers
0
Namespaces aren't ANSI, .... yet. But I would get used to seeing them.

Also, "endl" is probably just defined as '\n';

#define endl '\n' // or something stupidly similar

And he does need the iostream.h header file, unless he plans on using those ugly namespace functions.

std::cout, std::cin, etc.

And that is just a console application, not a Win32 application. Don't get the two confused.
 

Users who are viewing this thread

Top Bottom