Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 31-01-2009
Member
 
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
Reply With Quote
  #2  
Old 31-01-2009
Member
 
Join Date: May 2008
Posts: 2,008
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.
Reply With Quote
  #3  
Old 31-01-2009
Member
 
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....
Reply With Quote
  #4  
Old 01-02-2009
Member
 
Join Date: May 2008
Posts: 2,008
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
Reply With Quote
  #5  
Old 01-02-2009
Member
 
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
Reply With Quote
  #6  
Old 02-02-2009
Member
 
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").
Reply With Quote
  #7  
Old 02-02-2009
Member
 
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~
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "C++ Array of Strings and Conversion !!"
Thread Thread Starter Forum Replies Last Post
Difference between two strings Aman 1 Software Development 5 03-12-2013 11:59 AM
Files and strings in C An1990 Software Development 2 10-05-2012 10:11 PM
How to use Strings in Python Edmund-Windows Software Development 5 31-12-2010 06:25 AM
Struct and Strings in C Ash maker Software Development 5 05-04-2010 12:31 PM
Array of Strings in C++ Jaden Software Development 3 25-10-2008 02:18 PM


All times are GMT +5.5. The time now is 10:58 AM.