Click here to Skip to main content
15,393,706 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Code:

#include <iostream>

using namespace std;

int main();
myNumber == '2';
{
   if (myNumber == '200')
 {
    return 0
 }
else
{
    myNumber + 2;
    cout << myNumber << endl;
}
}


What I have tried:

I've tried changing the variable
Posted
Updated 24-Jun-22 20:05pm

C++
mynumber == '2'
is an evaluation meaning is mynumber equal to the character '2'.

You have to declare the type of a variable before using it:
char mynumber = '2';

And keep in mind that you are using numbers as a char -- that is what the single-quotes are defining in your code.

Also, mynumber == '200' will not work. It's incorrect syntax.
You really want to declare the myNumber as an integer type, like:
int myNumber = 2; // no single-quotes.
You probably want something like:
C++
int main()
{
   int myNumber = 2;
   if (myNumber == 200)
   {
     return 0;
   }
  else
  {
    myNumber += 2;
    cout << myNumber << endl;
  }
} // end of main


Note: this code only runs one time, because there is no looping mechanism (while or for loop).
   
Couple of reasons for this.
0) You don't need a semicolon here:
C++
int main();

1) Your curly bracket is in the wrong place:
C++
int main();
myNumber == '2';
{
It should be immediately below the function declaration:
C++
int main()
   {
   myNumber == '2';

2) myNumber is not defined, so it doesn't have any type that C++ can know. Try
C++
int myNumber == '2';

3) "==" is a comparison operator, not an assignment, so even if you had declared it myNumber would be have no "real" value. Use "=" instead - that's the assignment operator.
4) '2' is a character, not an integer - you probably want to use numbers here.
5) '200' is not a character, it's three characters - so that won't compile either, and you can't compare that with a single character even if you wanted to.
6)
C++
myNumber + 2;
Does nothing: it does not change any values in the program at all. Probably, you meant
C++
myNumber += 2;
Or
C++
myNumber = myNumber + 2;

7) What did you expect '2' plus 2 to give you? Would it still work if you had assigned '9' instead?
8) Indent your code! pick a style, and stuck to it - poor indentation makes code harder to read, and that means it's harder to get right.

So little code, so many errors! :laugh:
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900