else and if statement PROBLEM

Feb 15, 2009 at 12:57am
Okay, I'm new at C++ and I'm going through some tutorials.
I tend to copy my tutorials down and compile them to see if it works. And this is not compiling.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Score Rater
// Dmonstrates the if statement

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
   int score;
   cout << "Enter your score: " << endl;
   cin >> score;
   
   if (score > 500)
      cout >> "\nWoah! Good job! Over 500!\n";
   else
       cout >> "\nUnder 500. Try again.\n";
   
   return 0;
}


It gives me an error message after the second cout:
no match for 'operator>>' in 'std::cout >> "\nWoah! Good job! Over 500!\n"'

Help me.
I'm using bloodshed dev c++
Last edited on Feb 15, 2009 at 12:58am
Feb 15, 2009 at 1:02am
Your cout operators are pointing the wrong way.
Try these: <<
;)

To remember these I have always used the analogy of cout "pipes data out" and cin "pipes data in". My programming relative told me that and not once have I had that problem.

Btw I suggest you find a new set of tutorials. http://www.cplusplus.com/doc/tutorial/
Last edited on Feb 15, 2009 at 1:08am
Feb 15, 2009 at 1:13am
My god, how blind was I? lol Thanks for pointing out the obvious problem!

Kind of following from a book for a class. Can't really change tutorials.

Also, what's the best way for the console app to not close on me as soon as I input something when it's running?

I used to put
1
2
3
 
char c;
cout >> c;


right before my last code block to prevent the app from closing on me. But it doesn't seem to work anymore.
Last edited on Feb 15, 2009 at 1:18am
Feb 15, 2009 at 1:51am
Hint: Are you sure cout is used for input?
Feb 15, 2009 at 2:05am
Once again, stupid mistake! This is hilarious.
Alright, thanks eker.
See, that's what happens when you stop coding for a few days when you're a noob like me.
Last edited on Feb 15, 2009 at 2:05am
Feb 15, 2009 at 2:14am
Ok, I'm onto another exercice.
The coding works just fine, but as soon as I do enter the correct number, it closes without letting me see the last cout statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Guess My Number
// The classic number guessing game

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
srand(time(0)); // seed random number generator

int theNumber = rand()%100 + 1; //random number between 1 and 100
int tries = 0, guess;

cout << "\tWelcome to Guess my Number\n\n";

do
{
     cout << "Enter a guess: ";
     cin >> guess;
     ++tries;
     
     if (guess > theNumber)
        cout << "Too high!\n\n";
        
     if (guess < theNumber)
        cout << "Too low!\n\n";
} while (guess != theNumber);

cout << "\nThat's it! You got it in " << tries << " guesses!\n";

return 0;
    
    char c;
    cin >> c;
}


UPDATE: OK, I removed the return 0; and now it works.
Why does it do that though?
Last edited on Feb 15, 2009 at 2:15am
Feb 15, 2009 at 8:55am
What do you think a return statement does??

Last edited on Feb 15, 2009 at 8:59am
Feb 15, 2009 at 7:44pm
"Return statement:
The return statement stops execution and returns to the calling function. When a return statement is executed, the function is terminated immediately at that point, regardless of whether it's in the middle of a loop, etc. "

I'm just a n00b after all.
Topic archived. No new replies allowed.