• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

(C++) Problem

Status
Not open for further replies.
Level 13
Joined
Jul 16, 2012
Messages
679
Hello Hivers :)

I have a problem...

Look this....

JASS:
#include<conio.h>
#include<iostream.h>

main()
{
    cout<<"Enter your number: "; /* Only 1, 2, 3 */
    cin>>o;

    int counter = 1, o; 

    if( o == 0){
      counter = 0;
    }else if( o == 1){ 
      q1++;
      t = t + c1; 
    }else if( o == 2){
      q2++;
      t = t + c2;
    }else if( o == 3){
      q3++;
      t = t + c3;
    }else if( o != 1 && o != 2 && o != 3){
      cout<<"Invalid: "<<o;
    }else
      cout<<"Invalid: "<<o; /*Non-numbers */
    getch();
    return 0;
}

So...
I want to get the 1, 2, 3
If the cin get letters or symbols

must be show Invalid but does not work :(
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Please. Learn. English.

You are missing using std;.
Your main doesn't return int in the declaration.
You are reading into a variable called o before you declare it.
You are using variables called q1, q2, q3, c1, c2, c3 and t, without ever declaring any of them.
You are assuming number characters match the actual numbers, which is obviously not right. If you want to compare a number against the ASCII identifier of that number, use id literals. For example, o == '1'.
Your 5th condition isn't necessary. If you got to it, you already know that o isn't 1, 2, or 3.
Your last condition will never be reached, it's pointless.
 
Level 13
Joined
Jul 16, 2012
Messages
679
@GhostWolf
Please. Learn. English.
? Im sorry for my bad english -_-

You are missing using std; .
Your main doesn't return int in the declaration.
You are reading into a variable called o before you declare it.
You are using variables called q1 , q2 , q3 , c1 , c2 , c3 and t , without ever declaring any of them.
btw, this is only a demo.

You are assuming number characters match the actual numbers, which is obviously not right. If you want to compare a number against the ASCII identifier of that number, use id literals. For example, o == '1'
yep, if I use char just like this
JASS:
if( o == '1'){
   bla bla
}else if( o == '2'){
   bla bla
   }
but how if o is 100?
Thats why I need help...

@edo
#include <iostream.h> is also wrong, it has to be #include <iostream> (proof: http://stackoverflow.com/questions/2...-vs-iostream-h )
what is a problem in
JASS:
#include<iostream.h>
?

also, it should be using namespace std ; not using std;
??

@DSG
switch is better...

Thanks...
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
What's a "demo", your code shouldn't even compile.

If a char variable is equal to 100, it's equal to the ASCII ID 'd'.
If your purpose is not to read a character, but rather a number, then read a number and not a character.
C++:
int number;
cin >> number;
This will automatically convert whatever input the user gives into a number, assuming the input is indeed a number.

If you are using functions from the std namespace, you either need to prepend them with std:: to scope them, or you need to add using namespace.
C++:
// No using, have to scope them explicitly
std::cout << ...;
std::cin >> ...;

// Using std, the scopes are implicit
using namespace std;
cout << ...;
cin >> ...;

// Using only cout, so cout can be implicit, but cin must be explicit
using std::cout;
cout << ...;
std::cin >> ...;
The same rules apply to any namespace, obviously.

As to <iostream>, edo494 linked to a page that explains why it should be used...
 
Last edited:
Status
Not open for further replies.
Top