Results 1 to 7 of 7

Thread: C++ Array of Strings and Conversion !!

  1. #1
    Join Date
    Jan 2009
    Posts
    4

    C++ Array of Strings and Conversion !!

    Hi, I have just wrote a program in c++ using visual studio 2008. It compiles, runs, and displays the results. However, it shows funky characters in between my two output strings. Here is my code (Comment on top explains what this code is suppose to do):

    // This program will allow users to input a string with maximum length of 512.
    // First, it will change all uppercase input to lowercase input.
    // Then, it will encode the inputed string using the function below.
    // The output will display original string and its encoded string

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <iomanip>
    #include <stdio.h>
    #include <ctype.h>

    using namespace std;

    const unsigned maxreclen = 512;

    int main () {

    char userinput[maxreclen + 1];
    char conversion[maxreclen + 1];
    cout << "Please input your string: " << endl;
    cin.getline (userinput,maxreclen);

    for (int i=0; i < strlen(userinput); i++) {
    if ((userinput[i] >=65) && (userinput[i] <= 90))
    conversion[i] = userinput[i] + 32;
    else
    conversion[i] = userinput[i];
    }

    char original[] = "abcdefghijklmnopqrstuvwxyz";
    char coded[] = "jmartyvwbdlqncxgzekipufohs";
    char temp[maxreclen+1];

    for (int i = 0; i < strlen(conversion); i++) {


    string letterfinder(original);

    char x = letterfinder.find(conversion[i]);

    temp[i] = coded[x];

    }

    cout << "Your input is: " << endl;
    cout << userinput << endl;
    cout << "Lowercase: " << endl;
    cout << conversion<< endl;
    cout << "Coded: " << endl;
    cout << temp << endl;

    return 0;

    }

    Can anyone help me identify my mistake??

    Thank you

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: C++ Array of Strings and Conversion !!

    I have just wrote a program in c++ using visual studio 2008. It compiles, runs, and displays the results. However, it shows funky characters in between my two output strings.
    can you give the excat line no where you getting the error or output where funky characters are diplaying.this will help me to slove the debug in program.

  3. #3
    Join Date
    Jan 2009
    Posts
    4

    Re: C++ Array of Strings and Conversion !!

    For example, if I input "Matthew", the output show will be:

    Your input is:
    Matthew
    Lowercase:
    matthew ..............(bunch of funky characters)...?atthew
    Coded:
    njiiwtf .......... (bunch of funky characters)......?iwwfwbdlqncxgzekipufohs
    Press any key to continue . . .

    So it seems like my lowercase string and temp strings contains some
    characters other than my standard input....

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: C++ Array of Strings and Conversion !!

    Quote Originally Posted by bihangteen View Post
    For example, if I input "Matthew", the output show will be:

    Your input is:
    Matthew
    Lowercase:
    matthew ..............(bunch of funky characters)...?atthew
    Coded:
    njiiwtf .......... (bunch of funky characters)......?iwwfwbdlqncxgzekipufohs
    Press any key to continue . . .

    So it seems like my lowercase string and temp strings contains some
    characters other than my standard input....
    little more difficult to interpret your code

  5. #5
    Join Date
    Jan 2009
    Posts
    4

    Re: C++ Array of Strings and Conversion !!

    Ok, after doing some debugging, I have found out that my Maxreclen = 512 causes my character array to have size of 512. The funky characters were representing the empty slots in the character array.

    So then my question is, how can I truncate my userinput[] array to inputted size? I still need that maxreclen = 512 to test if user input array does not exceeds 512 characters.

    Thank you for your replies

  6. #6
    Join Date
    May 2008
    Posts
    945

    Re: C++ Array of Strings and Conversion !!

    Why not use simply like this:

    Code:
    if (userinput == NULL || maxreclen <= 0)
    return;
    if (maxreclen < strlen(userinput))
    just after the user inputs the characters (i.e. after "cin").

  7. #7
    Join Date
    Jan 2009
    Posts
    4

    Re: C++ Array of Strings and Conversion !!

    Thank you for the great advise. Since I can only use what I have learned so far from the class, I simply created new string variable and stored char varaibles into the new strings. And it worked fine. I'll keep your advice in mind for future reference Lemog. Thanks~

Similar Threads

  1. Difference between two strings
    By Aman 1 in forum Software Development
    Replies: 5
    Last Post: 03-12-2013, 11:59 AM
  2. Files and strings in C
    By An1990 in forum Software Development
    Replies: 2
    Last Post: 10-05-2012, 10:11 PM
  3. How to use Strings in Python
    By Edmund-Windows in forum Software Development
    Replies: 5
    Last Post: 31-12-2010, 06:25 AM
  4. Struct and Strings in C
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 05-04-2010, 12:31 PM
  5. Array of Strings in C++
    By Jaden in forum Software Development
    Replies: 3
    Last Post: 25-10-2008, 02:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,012,548.69064 seconds with 17 queries