• Welcome to Valhalla Legends Archive.
 

problem with simple prog

Started by Hamtaro, February 13, 2004, 04:07 PM

Previous topic - Next topic

K


#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;
}

Hamtaro

thx  :)
and now i pushed "new source file" instead of just starting with a blank project and it includes the system("PAUSE"); already in it.  what about boolalpha? does that not work anymore?

Skywing

Quote from: Dyndrilliac on February 14, 2004, 02:55 PM
Don't getch() and pause do the same effect?
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.

Hamtaro

Quote from: Skywing on February 15, 2004, 12:07 PM
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.
what include file do i use for getc(stdin)?

Skywing

Use <stdio.h> for getc and <wchar.h> for getwc.

Hamtaro

#20
thx, skywing.
+1 :)

i like the getc(stdin)
what does the "stdin" part mean?

iago

Quote from: Skywing on February 15, 2004, 12:07 PM
Quote from: Dyndrilliac on February 14, 2004, 02:55 PM
Don't getch() and pause do the same effect?
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.

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Kp

#22
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]
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Skywing

#23
Quote from: Kp on February 15, 2004, 01:47 PM
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]
Try using 2>..

CrAzY

Quote
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) cout << "number1 is equal to 22";
return=0;
}

Try


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

iago

Quote from: CrAzY on February 16, 2004, 07:39 AM
Try

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


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?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Dyndrilliac

Quote from: CrAzY on February 16, 2004, 07:39 AM
Quote
#include <iostream>
int main()
{
using namespace std;
int number1 = 22;
if (number1 == 22) cout << "number1 is equal to 22";
return=0;
}

Try


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


Not to mention the "return=0;" will cause an error. It's supposed to be "return 0;"
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

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.

Skywing

#28
Quote from: iago on February 16, 2004, 09:21 AM
Quote from: CrAzY on February 16, 2004, 07:39 AM
Try

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


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?
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.

iago

Quote from: Skywing on February 16, 2004, 12:47 PM
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.

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?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


|