-2

I am finding difficulty declaring this.

please see what I am writting.

int choice:>=1&&<=100

by writting the above I wish to use later in the code that a user will input a choice starting from (1-100) meaning will input any integer from 1-100.

However when writting the above declaration and initialization i get the erro messageL

38 15 G:\Welcome Message.cpp [Error] expected initializer before ':>' token

can someaone do explain why I am getting the erro and how to I write correctly the declaration and initialization above.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Surdz
  • 77
  • 3
  • 1
    I think you need to ask your instructor for help (if applicable) or read a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – qxz Mar 07 '17 at 05:34
  • 1
    Aside from the problems that the answers mention, `:>` is a **digraph**; in essence, it gets replaced by the single character `]`. – Pete Becker Mar 07 '17 at 05:39

2 Answers2

3

You've confused datatype declarations, user input, and range-testing. These are not magic C++ statements -- they are three separate things.

int choice;
if( std::cin >> choice && choice >= 1 && choice <= 100 )
{
    std::cout << "Valid choice: " << choice << std::endl;
}
else
{
    std::cout << "Invalid choice" << std::endl;
}
paddy
  • 60,864
  • 6
  • 61
  • 103
1
int choice:>=1&&<=100.

You're not really "comparing" anything. When you do this, what you want to do is, Does Is choice greater than or equal to 1, AND is choice less than or equal to 100. What your code is saying is more "human" and natural. IS choice greater than or equal to 1, AND less than or equal to one. But remember, our computers are pretty dumb, so you need to declare it more precisely, like so...

choice >= 1 && choice <= 100

Also, make sure to declare choice somewhere else. Like so...

int choice = 5;
...
if(choice >= 1 && choice <= 100){
   ...
}
Matt I
  • 165
  • 3
  • 11