0

Hey guys, i am having a problem solving a this part of my coding. Basically, what im doing is for the program to read a string, and if there is a value of 'M', the value of num will go up by 1000.

Here my coding ....

string msg ;

    int n = 0;
    int num ;

     cout << "Enter the letter please ";
	cin >> msg ;

        const char* numeral = msg.c_str ();
        cout << numeral << endl;
        if (numeral[n] =='M')
        {
            num += 1000
            n++;
            
            cout << num << endl;
        }

So, when i input in "MM" into the program, theorywise, my num value should be 2000 right? but for some reason, i keep getting a weird number .

Basically, my expected output will be, if there is 'a' amount of M in a string, the output will be (a * 1000). Could somebody point out my mistake in this.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by gregarion
0

>int num ;
Don't forget to initialize your variables. This should be int num = 0; since all you're doing afterward is adding to it. That's the cause of your weird numbers.

>if (numeral[n] =='M')
There's no loop here, so you'll only test numeral[0] .

>cout << num << endl;
Assuming you really wanted a loop, this statement should be outside of the body. Otherwise you'll be printing all of the intermediate values of num .

0

Thank alot! cant believe i missed that int num = 0 ; part. thank again

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.