Code::Blocks Forums

User forums => Help => Topic started by: Yang_CH on October 26, 2019, 03:37:22 pm

Title: what is wrong with my code?
Post by: Yang_CH on October 26, 2019, 03:37:22 pm
Hello, Everyone,
My c++ code is as below, my input is: 10.0.3.193, the output of which is 192. But with other IDE and the the same code and input, I got output of 193, which is what I expect. what is the cause? Thanks!
//----------------------------------------------------------------
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
    string str;
    cin>>str;
    int temp=0;
    for(int i=str.rfind('.')+1;i<str.length();i++)
    {
        temp+=(str-'0')*pow(10,str.length()-i-1);
    }
    cout<<temp<<endl;
    return 0;
}
//----------------------------------------------------------------
Title: Re: what is wrong with my code?
Post by: cacb on October 26, 2019, 04:48:53 pm
This is not a general programming board, read this
http://forums.codeblocks.org/index.php/topic,9996.0.html
Title: Re: what is wrong with my code?
Post by: BlueHazzard on October 27, 2019, 07:36:37 pm
Quote
My c++ code is as below, my input is: 10.0.3.193, the output of which is 192. But with other IDE and the the same code and input, I got output of 193, which is what I expect. what is the cause? Thanks!
i do not think so.. Your code is wrong on so many levels.... I even do not understand how you can compile this. Your posted code is not valid c++ code....
Title: Re: what is wrong with my code?
Post by: sodev on October 27, 2019, 10:08:52 pm
The "code" suddenly starts to become italic, im pretty sure some array indices became formatting commands, thats why you should post code always in code tags.

So this might compile, but i do not know and do not want to know what this is supposed to do.
Title: Re: what is wrong with my code?
Post by: sodev on November 27, 2019, 12:36:12 am
Why i can't see any difference between the bug and your correction? Why i can see italics again?

This is getting really scary...
Title: Re: what is wrong with my code?
Post by: RRomano001 on November 27, 2019, 12:44:34 am
Why i can't see any difference between the bug and your correction? Why i can see italics again?

This is getting really scary...
Sorry after realizing what it was wrong with code I decided remove message due it violate rules.
 Again italic come from the code of poster.
 This forum has no preview mode and posted text was the only help discovering what was wrong on original code. str-'0' extract one character by using i as index, square braces used for array indexing where interpreted as italic command.
 Original poster problem remain unaddressed but answer where more confusing than original post.
 Code has nothing wrong in it than how is there due to "["i"]" translation to italic.
 Sometimes explain how to use "code tags" is better than just do confuse more who is reading about.
 As I wrote on erased message:
 Newbie forever discover hidden feature of environment.