Author Topic: what is wrong with my code?  (Read 4045 times)

Yang_CH

  • Guest
what is wrong with my code?
« 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;
}
//----------------------------------------------------------------
« Last Edit: October 26, 2019, 03:40:18 pm by Yang_CH »

Offline cacb

  • Lives here!
  • ****
  • Posts: 536
Re: what is wrong with my code?
« Reply #1 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

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: what is wrong with my code?
« Reply #2 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....

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: what is wrong with my code?
« Reply #3 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.

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: what is wrong with my code?
« Reply #4 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...

Offline RRomano001

  • Single posting newcomer
  • *
  • Posts: 9
Re: what is wrong with my code?
« Reply #5 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.
« Last Edit: November 27, 2019, 12:55:08 am by RRomano001 »