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
Re: C++ Array of Strings and Conversion !!
Quote:
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.
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....
Re: C++ Array of Strings and Conversion !!
Quote:
Originally Posted by
bihangteen
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 :blink:
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 :crybaby:
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").
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~:thumbup1: