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.

>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 .

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.