1,105,375 Community Members

self-made converter

Member Avatar
guy21
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
void main() {

char line[300];


cout<<"enter ascii code";
cin>>line;

int d = strlen(line);

for(int i=0; i<d-1; i+4) {

if(line[i]!='.') {

(int)line[i];
(int)line[i+1];
(int)line[i+2];

int num=(line[i]-48)*100+(line[i+1]-48)*10+line[i+2]-48;
cout>>(char)num;
}
}
getch();
}

ok, you enter ascii codes like 104.101.121. and the program converts this into a line like 'hey'. But there's something wrong with the code and the program gives me 'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh'. What's wrong with it?

Member Avatar
Ancient Dragon
Achieved Level 70
27,612 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,033 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

line 11: i+4 should be i+=4, the value of i is not getting incremented in your program.

Member Avatar
Gonbe
Posting Whiz in Training
286 posts since Jan 2010
Reputation Points: 32 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 12 [?]
 
1
 

There's a bunch of things to be said about your code. Below is your code with some comments, I didn't change anything though.

#include <iostream>
#include <string.h> // C-header, iew!

using namespace std;

int main()
{
    // We're using C++, use a string object instead of a char array.
    char line[300];

    // I assume you specified the input as "xxx.xxx" and so on because you had issues
    // with spaces? For a C++ solution, look into the getline function from the string library:
    // http://www.cplusplus.com/reference/string/getline/
    cout<<"enter ascii code";
    cin>>line;

    int d = strlen(line);

    // I wouldn't increment 1 by 4 (given you'd fix the "i + 4" -> "i += 4" part), wouldn't "40" or whatever be valid, now you'd have to enter it as "040"?
    // Also watch out with the ending condition "d-1" should be d, although it does not 
    // matter due to the way you increment right now. But I can only imagine it is unintended.
    for(int i=0; i<d-1; i+4)
    {
        // This check isn't needed as you are already making assumptions about your input.
        // You're already assuming every ASCII value consists of 3 symbols and that there is
        // exactly 1 dot between them. You will always end up on a decimal. The idea to check
        // the input is good though! Just try to modify the loop condition first.
        if(line[i] != '.') 
        {
            // These statements are useless, they are also not very error proof depending on your input. (i+2)
            (int)line[i];
            (int)line[i+1];
            (int)line[i+2];

            // 48 is the ASCII value for '0', it's more readable if you add it like that.
            // More importantly if you read the values as ints you can cast them to char directly and you don't need to do this.
            int num=(line[i]-48)*100+(line[i+1]-48)*10+line[i+2]-48;

            // You use the wrong operator here, use "<<".
            cout>>(char)num;
        }
    }

    return 0;
}

You should probably re-write it from scratch. Another thing that was confusing to me was the example you give in your OP which converts ASCII values to letters while you code seems to be made to convert the supplied input to integers.

Member Avatar
guy21
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you guys! :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article