Valhalla Legends Forums Archive | C/C++ Programming | problem with simple prog

AuthorMessageTime
Hamtaro
this is the code im using:
[code]
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) cout << "number1 is equal to 22";
return=0;
}
[/code]

and its saying "parse error before `=' " on line 7 (return=0;)
i cant figure out what it means.. any help? sry to bug u with such simple code but i cant figure this out ^^;.. im using the Dev-C++ compiler could that be the problem?
February 13, 2004, 10:07 PM
Kp
Why're you assigning zero to a reserved keyword? Also, there's no need to use that if test -- you're guaranteeing that the number will be 22.
February 13, 2004, 10:09 PM
Hamtaro
lol.. thanx

its supposed to be
[code]
return 0;
[/code]
didnt mean for it to be a variable. im just trying to get this to work bcuz when i execute the progs it closes it as soon as it finishes and i havnt gone deep enough to make it ask me to push a button to end it so im using the "cin" to delay it. then i wanted to turn the cin's input into a variable and output that variable and it wouldnt work so i went back to the even more basic basics. if u dont understand thats ok ^^;
February 13, 2004, 10:28 PM
Kp
[quote author=Hamtaro link=board=30;threadid=5279;start=0#msg44096 date=1076711291]didnt mean for it to be a variable. im just trying to get this to work bcuz when i execute the progs it closes it as soon as it finishes and i havnt gone deep enough to make it ask me to push a button to end it so im using the "cin" to delay it. then i wanted to turn the cin's input into a variable and output that variable and it wouldnt work so i went back to the even more basic basics. if u dont understand thats ok ^^;[/quote]

It would help greatly if you actually put some effort into being understandable. It has been my experience that program consoles don't go away if you actually run the program from a console like you're supposed to. Perhaps you were doing something foolish like double clicking it in Explorer to run it?
February 13, 2004, 10:30 PM
Hamtaro
[quote]It would help greatly if you actually put some effort into being understandable. It has been my experience that program consoles don't go away if you actually run the program from a console like you're supposed to. Perhaps you were doing something foolish like double clicking it in Explorer to run it?
[/quote]
i dont understand?
i think u mean running it from the cmd prompt? ya it doesnt when i do that but if theres a button to run it in the compiler itself.. why not use that? i see what you mean now ^^;
February 13, 2004, 10:39 PM
MoNksBaNe_Agahnim
[quote author=Hamtaro link=board=30;threadid=5279;start=0#msg44092 date=1076710024]
this is the code im using:
[code]
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) cout << "number1 is equal to 22";
return=0;
}
[/code]
[/quote]

shouldn't you declare namespaces outside of functions, like you would #include files and global variables? Haven't had much practice with namespaces but every example I have seen has them declared as...

[code]
#include <iostream.h>

using namespace std;

int main()
{
}
[/code]
February 13, 2004, 11:03 PM
Kp
[quote author=Hamtaro link=board=30;threadid=5279;start=0#msg44099 date=1076711996]if theres a button to run it in the compiler itself.. why not use that?[/quote]

because it closes when the program exits, as you told us in your prior post? Seems pretty obvious that if doing it that way causes a problem reading the output before it vanishes, you shouldn't do it that way!
February 13, 2004, 11:04 PM
Hamtaro
ya.. ive been looking since u said that for a button to just plain compile it bcuz its just compiling/running in memory and not saving the exe anywhere that i can tell..

about using namespaces.. i dont know =| this book is preaching that if u dont need to use it (in this case, namespaces) outside of one function, then you shouldnt declare it globally.

i think i said that right :)
February 13, 2004, 11:28 PM
Kp
[quote author=Hamtaro link=board=30;threadid=5279;start=0#msg44110 date=1076714926]
ya.. ive been looking since u said that for a button to just plain compile it bcuz its just compiling/running in memory and not saving the exe anywhere that i can tell..[/quote]

It takes some pretty fancy code work to run a full executable without ever writing it to disk (it's much easier to do with DLLs). I suspect you're just unable to find the save directory. Check where you told the compiler to save it? I prefer using makefiles, so I don't use dev-C++.
February 14, 2004, 12:13 AM
Hamtaro
no~ it saves it in .cpp as c++ source <-- may be a common thing i dont know.. and i found out that if you have an error and try to compile it, it erases the exe for some reason.. =\ oh well i have borland too but its really old and u have to put .h after include files in it and u dont in dev so thats why i chose it in the beginning =P (i figured out later about the .h)
February 14, 2004, 12:46 AM
Dyndrilliac
If you want to make the program pause and wait for a keystroke before moving on to the next statement, do this:

[code]#include <iostream.h>
#include <conio.h>

int main
{

int x;
int y;

cout<<"Your text here, b;ah blah blah"<<endl;

getch(); // pauses and waits for a keystroke before movin on

return 0;
}[/code]
February 14, 2004, 3:37 PM
iago
The easiest (but, as Skywing will say, not the best) way to do it is:

#include <stdlib.h>

....

system("pause");
return 0;
}
February 14, 2004, 5:35 PM
Skywing
[quote author=iago link=board=30;threadid=5279;start=0#msg44274 date=1076780132]
The easiest (but, as Skywing will say, not the best) way to do it is:

#include <stdlib.h>

....

system("pause");
return 0;
}
[/quote]
Easiest? getch() is less typing!
February 14, 2004, 8:08 PM
Dyndrilliac
Don't getch() and pause do the same effect?
February 14, 2004, 8:55 PM
Hamtaro
thx ^^
how do i take whats inputted and turn it into a variable?

is it:
[code]
#include <iostream.h>
#include <string.h>

int main()
}
using namespace std;
string text;
cin << text;
return 0;
}
[/code]
i may have to include another file let me see..
yep.. <string>

also in this:
[code]#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
cout << boolalpha << (number1 == 22);
return 0;
}[/code]

its exactly like the book says and its saying i need to declare boolalpha but the book doesnt. its supposed to give me either "false" or "true"
February 14, 2004, 9:38 PM
K
[code]
#include <iostream>
#include <string>

using namespace std;

int main()
{
string sInput;
// this will read one word.
cin >> sInput;
cout << "you entered: " << sInput << endl;

// this will read a whole line.
getline(cin, sInput);
cout << "you entered: " << sInput << endl;

// this will hold the window open until you press enter.
getline(cin, sInput);
return 0;
}
[/code]
February 14, 2004, 9:53 PM
Hamtaro
thx :)
and now i pushed "new source file" instead of just starting with a blank project and it includes the [code]system("PAUSE");[/code] already in it. what about boolalpha? does that not work anymore?
February 14, 2004, 10:25 PM
Skywing
[quote author=Dyndrilliac link=board=30;threadid=5279;start=0#msg44326 date=1076792111]
Don't getch() and pause do the same effect?
[/quote]
Actually, you should use getc(stdin) and not getch() - sorry about that.

The reason is that getc(stdin) has a well-defined effect across all platforms, and system("pause") does not.
February 15, 2004, 6:07 PM
Hamtaro
[quote author=Skywing link=board=30;threadid=5279;start=15#msg44440 date=1076868472]
Actually, you should use getc(stdin) and not getch() - sorry about that.

The reason is that getc(stdin) has a well-defined effect across all platforms, and system("pause") does not.
[/quote]
what include file do i use for getc(stdin)?
February 15, 2004, 6:19 PM
Skywing
Use <stdio.h> for getc and <wchar.h> for getwc.
February 15, 2004, 6:47 PM
Hamtaro
thx, skywing.
+1 :)

i like the getc(stdin)
what does the "stdin" part mean?
February 15, 2004, 7:02 PM
iago
[quote author=Skywing link=board=30;threadid=5279;start=15#msg44440 date=1076868472]
[quote author=Dyndrilliac link=board=30;threadid=5279;start=0#msg44326 date=1076792111]
Don't getch() and pause do the same effect?
[/quote]
Actually, you should use getc(stdin) and not getch() - sorry about that.

The reason is that getc(stdin) has a well-defined effect across all platforms, and system("pause") does not.
[/quote]

when I'm doing cross-platform coding, I do this:
#ifdef WIN32
system("pause");
#endif
simply because I already run Linux programs in a console.


stdin means standard in, it's generally the keyboard unless it's redirected, but that's a different story. There's also stdout, which is standard output, typically your console, and stderr, which is standard error, and is the same as standard output normally, unless stdout is redirected.
February 15, 2004, 7:40 PM
Kp
stderr can also be redirected, unless you use some really bad shell like cmd.exe. It's programmatically possible even on Win32, but afaik cmd.exe doesn't support doing it. See dup and dup2 on Linux or SetStdHandle on Win32.

[edit: typo]
February 15, 2004, 7:47 PM
Skywing
[quote author=Kp link=board=30;threadid=5279;start=15#msg44460 date=1076874430]
stderr can also be redirected, unless you use some really bad shell like cmd.exe. It's programmatically possible even on Win32, but afaik cmd.exe doesn't support doing it. See dup and dup2 on Linux or SetStdHandle on Win32.

[edit: typo]
[/quote]
Try using 2>..
February 16, 2004, 1:02 AM
CrAzY
[quote]
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) cout << "number1 is equal to 22";
return=0;
}
[/quote]

Try

[code]
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) {
cout << "number1 is equal to 22\n";
}else{
return=0;
}[/code]
February 16, 2004, 1:39 PM
iago
[quote author=CrAzY link=board=30;threadid=5279;start=15#msg44567 date=1076938786]
Try
[code]
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) {
cout << "number1 is equal to 22\n";
}else{
return=0;
}[/code]
[/quote]

umm, no? If the number is 22, main doesn't bother returning? On some compilers that won't even compile. Otherwise, something unpredictable happens, or garbage gets returned. Plus, if there's only one line on an if, you don't need braces.

Finally, that wasn't even his problem; his problem was that his program wasn't pausing before exiting. I don't see how only returning sometimes and adding braces could cause the program to pause. Perhaps you would like to explain how?
February 16, 2004, 3:21 PM
Dyndrilliac
[quote author=CrAzY link=board=30;threadid=5279;start=15#msg44567 date=1076938786]
[quote]
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) cout << "number1 is equal to 22";
return=0;
}
[/quote]

Try

[code]
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) {
cout << "number1 is equal to 22\n";
}else{
return=0;
}[/code]
[/quote]

Not to mention the "return=0;" will cause an error. It's supposed to be "return 0;"
February 16, 2004, 4:46 PM
Hamtaro
the return=0; was my fault. that was my original problem. and then after that was solved, i asked how to pause it. i did find a way to pause it and its been answered on how to turn what i inupt into output. thanks. this thread may be closed.
February 16, 2004, 6:09 PM
Skywing
[quote author=iago link=board=30;threadid=5279;start=15#msg44573 date=1076944873]
[quote author=CrAzY link=board=30;threadid=5279;start=15#msg44567 date=1076938786]
Try
[code]
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) {
cout << "number1 is equal to 22\n";
}else{
return=0;
}[/code]
[/quote]

umm, no? If the number is 22, main doesn't bother returning? On some compilers that won't even compile. Otherwise, something unpredictable happens, or garbage gets returned. Plus, if there's only one line on an if, you don't need braces.

Finally, that wasn't even his problem; his problem was that his program wasn't pausing before exiting. I don't see how only returning sometimes and adding braces could cause the program to pause. Perhaps you would like to explain how?
[/quote]
main is a special case; you are not required to return a value for it. This is not true for any other function with a non-void return type.
February 16, 2004, 6:47 PM
iago
[quote author=Skywing link=board=30;threadid=5279;start=15#msg44606 date=1076957232]
main is a special case; you are not required to return a value for it. This is not true for any other function with a non-void return type.
[/quote]

Your program will compile and run without main returning a value, but isn't it supposed to per the standard? If you aren't going to return, why don't you just declare it void?

If you don't specify a value, what is automatically returned?
February 16, 2004, 7:43 PM
K
[quote author=iago link=board=30;threadid=5279;start=15#msg44616 date=1076960596]
[quote author=Skywing link=board=30;threadid=5279;start=15#msg44606 date=1076957232]
main is a special case; you are not required to return a value for it. This is not true for any other function with a non-void return type.
[/quote]

Your program will compile and run without main returning a value, but isn't it supposed to per the standard? If you aren't going to return, why don't you just declare it void?

If you don't specify a value, what is automatically returned?
[/quote]

According to the newest standard, if you omit a "return" from main, main is assumed to return 0.
February 16, 2004, 8:38 PM
Skywing
[quote author=iago link=board=30;threadid=5279;start=15#msg44616 date=1076960596]
[quote author=Skywing link=board=30;threadid=5279;start=15#msg44606 date=1076957232]
main is a special case; you are not required to return a value for it. This is not true for any other function with a non-void return type.
[/quote]

Your program will compile and run without main returning a value, but isn't it supposed to per the standard? If you aren't going to return, why don't you just declare it void?

If you don't specify a value, what is automatically returned?
[/quote]
No, the standard says that main() is an exception and does not have to return a value. I think 0 is assumed. You cannot declare it as void, however.
February 17, 2004, 3:29 AM
Hamtaro
where do you find these standards at?
February 17, 2004, 3:31 AM
MrRaza
Google, C++ Standards
February 18, 2004, 2:18 PM
Eli_1
haha Raza, I don't think I'v seen you respond to anything with other than 'Google it' ;D
February 18, 2004, 3:14 PM
Hamtaro
lol i did the google search before i asked the question :P

i found a standards site that was a .gov i think but i didnt actually see a document with the standards anywhere..
February 18, 2004, 10:39 PM
Grok
[quote author=Hamtaro link=board=30;threadid=5279;start=30#msg44687 date=1076988687]
where do you find these standards at?
[/quote]

Skywing has insider information.
February 18, 2004, 10:48 PM
Grok
[quote author=Grok link=board=30;threadid=5279;start=30#msg44921 date=1077144484]
[quote author=Hamtaro link=board=30;threadid=5279;start=30#msg44687 date=1076988687]
where do you find these standards at?
[/quote]

Skywing has insider information.
[/quote]

Hamtaro:

"The C++ Programming Language" by Bjarne Stroustrup for standards of C++.
February 19, 2004, 10:14 PM

Search