| View previous topic :: View next topic |
| Author |
Message |
famousmassacre1 Expert Cheater
Reputation: 0
Joined: 30 Jun 2008 Posts: 113 Location: San Antonio Texas
|
Posted: Wed Aug 06, 2008 9:28 am Post subject: C++ error |
|
|
i keep getting an error on this code but i cant find whats wrong
| Code: |
// This is a comment
// This program will accept and display their sum
#include <iostream.h>
void main()
{
// Define variables
float num1;
float num2;
float total;
// Enter data for variables
cout << "10";
cin >> num1;
cout << "9";
cin >> num2;
// Add the two numbers together
total = num1 + num2;
// Display the result
cout << "The sum of the numbers = " << total;
} | [/code]
_________________
Last edited by famousmassacre1 on Wed Aug 06, 2008 9:43 am; edited 1 time in total |
|
| Back to top |
|
 |
jackyyll Expert Cheater
Reputation: 0
Joined: 28 Jan 2008 Posts: 143 Location: here
|
Posted: Wed Aug 06, 2008 9:40 am Post subject: |
|
|
First, you should use [code] tags when posting code.. Second, when asking about an error you get, it is very helpful if you.. Post the error your getting..
So post the error you are getting and i will try and help you fix it..
|
|
| Back to top |
|
 |
famousmassacre1 Expert Cheater
Reputation: 0
Joined: 30 Jun 2008 Posts: 113 Location: San Antonio Texas
|
Posted: Wed Aug 06, 2008 9:42 am Post subject: |
|
|
how do i post the error i dont know he error i just cant compile it then run it
_________________
|
|
| Back to top |
|
 |
jackyyll Expert Cheater
Reputation: 0
Joined: 28 Jan 2008 Posts: 143 Location: here
|
Posted: Wed Aug 06, 2008 9:46 am Post subject: |
|
|
| Well, if you're using visual studio to compile it would show the error in the output panel near the bottom of the screen. So just copy paste that or write it out if it doesn't let you..
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Aug 06, 2008 9:51 am Post subject: |
|
|
View -> Error List.
or
Ctrl + W + E
_________________
|
|
| Back to top |
|
 |
Typhoon808 Expert Cheater
Reputation: 0
Joined: 27 Mar 2008 Posts: 175 Location: Wales
|
Posted: Wed Aug 06, 2008 9:54 am Post subject: |
|
|
The error is probably about undefined words, correct?
Add std:: to the beginning of cout.
|
|
| Back to top |
|
 |
famousmassacre1 Expert Cheater
Reputation: 0
Joined: 30 Jun 2008 Posts: 113 Location: San Antonio Texas
|
Posted: Wed Aug 06, 2008 10:05 am Post subject: |
|
|
should i get visual C++ cuz ppl here said its horrible
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Aug 06, 2008 10:07 am Post subject: |
|
|
| Typhoon808 wrote: | The error is probably about undefined words, correct?
Add std:: to the beginning of cout. | He's using iostream.h... And that's also his problem I guess. Modern compilers don't have that obsolete file anymore. | Code: | #include <iostream>
using std::cout;
using std::cin; | Btw.. Get a more recent book :P | Code: | #include <iostream>
int main(int argc, char *argv[])
{
float num1, num2;
std::cout << "Num1: ";
std::cin >> num1;
std::cout << "Num2: ";
std::cin >> num2;
std::cout << "The sum of the numbers is " << num1+num2;
return EXIT_SUCCESS;
} |
Last edited by Jani on Wed Aug 06, 2008 10:15 am; edited 1 time in total |
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Aug 06, 2008 10:14 am Post subject: |
|
|
Ahhhhhhh, you're using a very outdated tutorial. Find a new one before continuing.
| Code: | #include <iostream> //no need for .h
int main() //int, not void
//...etc |
cout and cin are part of the std namespace. In order to use them, you have to tell the compiler those are the functions you mean. You can either put
| Code: | | using namespace std; |
directly under your includes which will include the entire std namespace in your program which is entirely unnecessary. Or you can prefix cin an cout with std::. As in
| Code: | std::cout << "stuff";
std::cin >> avar; |
Which is a lot better once you get used to reading it that way.
You're also not pausing the program after it's done. Use
at the end of the program to wait for unput from the user before continuing.
You should also put
| Code: | | return EXIT_SUCCESS |
at the end of the program instead of just the closeing bracket.
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Aug 06, 2008 10:14 am Post subject: |
|
|
| famousmassacre1 wrote: | | should i get visual C++ cuz ppl here said its horrible |
Yes you should get it. It's probably the best compiler out there. And nobody here said VC++ sucks, they said Dev-C++ sucks. And you should find a new tutorial if they're teaching you to use iostream.h. And as people s tated your problem is you're not putting std:: in front of your cin and cout. The thing is the iostream header file has the standard namespace (std) in it, which holds the functions such as cout and cin. So you can either say you're going to use it with this line of code:
| Code: |
using namespace std; //your gonna use the namespace so you can do this
//now you dont have to use std::cout and std::cin
cout<<"bla";
cin>>bla;
|
Otherwise you have to put std:: in front of cout and cin so that your compiler knows your referencing functions from the standard namespace.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Wed Aug 06, 2008 10:17 am Post subject: |
|
|
| HalfPrime wrote: | | You're also not pausing the program after it's done. | Pausing is for stupid noobs who don't know how to use console >_> You should NEVER pause your program that way..
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Aug 06, 2008 10:18 am Post subject: |
|
|
How do you suggest then Jani? Because most people do use either this:
or this:
| Code: |
cin.sync();
cin.get();
|
And please don't say system("PAUSE").
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
famousmassacre1 Expert Cheater
Reputation: 0
Joined: 30 Jun 2008 Posts: 113 Location: San Antonio Texas
|
Posted: Wed Aug 06, 2008 10:33 am Post subject: |
|
|
i really dont understand i started C++ yesterday and im only 12 but have been programming for a long time
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Aug 06, 2008 10:37 am Post subject: |
|
|
Add me on msn and I'll explain the stuff you don't understand.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Aug 06, 2008 11:06 am Post subject: |
|
|
| Code: | std::cin.sync();
std::cin.ignore(); |
To pause.
_________________
- Retired. |
|
| Back to top |
|
 |
|