We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,039 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

self-made converter

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?

3
Contributors
3
Replies
1 Day
Discussion Span
5 Months Ago
Last Updated
4
Views
guy21
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

Ancient Dragon
Achieved Level 70
Team Colleague
32,128 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

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.

Gonbe
Posting Whiz in Training
231 posts since Jan 2010
Reputation Points: 33
Solved Threads: 29
Skill Endorsements: 9

thank you guys! :)

guy21
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0694 seconds using 2.72MB