characters (integer value)
change:
printf("%c", string);
to:
printf("%c", string);//<-page changes it to italics tag<br><br><br>and grav…gets() doesnt add a '\0' character? (asking)<br><br><SPAN CLASS=editedby>[edited by - kordova on March 4, 2003 9:10:58 PM]</SPAN>
printf("%c", string);
to:
printf("%c", string);//<-page changes it to italics tag<br><br><br>and grav…gets() doesnt add a '\0' character? (asking)<br><br><SPAN CLASS=editedby>[edited by - kordova on March 4, 2003 9:10:58 PM]</SPAN>
gets() does set a ''\0'' on the end and I believe its fgets that doesnt do that. So thats why I set a NULL value to the end, so that wasnt necessary.
If string[strlen(string)+ 1] = ''\0''; is taken out the for loop gets changed to
for(i=strlen(string);i>-1;i--)
When it added a space to the beginning that should have told me I didnt need to add the ''\0'' I was just too lazy to find out why it did it. Stupid mistake on my part.
for(i=strlen(string);i>-1;i--)
When it added a space to the beginning that should have told me I didnt need to add the ''\0'' I was just too lazy to find out why it did it. Stupid mistake on my part.
Awsome guys, thanks. How would I go about changing the inversed string into integers? I also can't get it to work properly when I try to type in that number into my other .cpp file (the decoder). Thanks.
[edited by - roble on March 4, 2003 11:32:03 PM]
[edited by - roble on March 4, 2003 11:34:11 PM]
[edited by - roble on March 4, 2003 11:34:41 PM]
[edited by - roble on March 4, 2003 11:35:07 PM]
//decoder.cpp// This is the 'decoder'. It unsrambles the code. The encoder//(if I get it to work properly, right now it just converts the//string into integers without the string being first inversed)//will hopefully output some numbers that hold the value of the //characters, and when put into the 'decoder', will turn those //integers into characters once again, and inverse them.#include <iostream.h>#include <string.h>int main (void){ int num; cout<<"Enter the numbers here:"; cin>>num; cout<<(char)num; return 0;}
[edited by - roble on March 4, 2003 11:32:03 PM]
[edited by - roble on March 4, 2003 11:34:11 PM]
[edited by - roble on March 4, 2003 11:34:41 PM]
[edited by - roble on March 4, 2003 11:35:07 PM]
I M Teh n00B11!!!1
Did you have a question in that last post? I compiled that code you just posted and it seems to work unless you want it to do something other than display the character that the input number represents.
Yeah I had one... There''s actually two parts to this:
In scramble.cpp
You know how I wanted to know how to reverse a string and all? Well, what I''m tring to do, is once that is reversed, I want to turn the character values of that reversed string into their integer values (With the integers representing the appropriate characters).
In descramble.cpp
I want to do the exact opposite. (Turning the integers back into the reversed string, and then reverse the string again so it is readible).
I actually have two questions now:
1) How do I turn the reversed string into the appropriate integer values?
and...
2) How do I create the syntax in "descramble.cpp"? (I know sort of the basis syntax, but get stupid little errors all the time).
In scramble.cpp
You know how I wanted to know how to reverse a string and all? Well, what I''m tring to do, is once that is reversed, I want to turn the character values of that reversed string into their integer values (With the integers representing the appropriate characters).
In descramble.cpp
I want to do the exact opposite. (Turning the integers back into the reversed string, and then reverse the string again so it is readible).
I actually have two questions now:
1) How do I turn the reversed string into the appropriate integer values?
and...
2) How do I create the syntax in "descramble.cpp"? (I know sort of the basis syntax, but get stupid little errors all the time).
I M Teh n00B11!!!1
#include <stdio.h>#include <string.h>// Global variable so you can use it in other functions// *NOTE this holds 80 integers int num[80]; // Same size of array int main( void ){ char string[80]; char *ptr; int i; printf("Enter a string:\n"); gets(string); for(i=(strlen(string) -1);i>-1;i--){ printf("%c", string); num = string<br>}<br>printf("\n");<br>// This loops through again and sends it back <br>// using num[] in foward order<br>for(i=0;i<(strlen(string));i++)<br> printf("%c", num);<br>printf("\n");<br><br>return 0;<br>}<br> </pre> <br><br>As far as your syntax problem…send what code you have and ill help you from there. </i>
I keep posting bad code....
for(i=(strlen(string) -1);i>-1;i--)
should be
for(i=strlen(string);i>-1;i--)
and you dont need char *ptr;
for(i=(strlen(string) -1);i>-1;i--)
should be
for(i=strlen(string);i>-1;i--)
and you dont need char *ptr;
quote: Original post by GravtyKlz
That wouldnt work because an array cant be = to an array.
This is true, but the AP wasn''t copying arrays. He was copying specific characters in each array, which is perfectly fine. I tested his code, and it didn''t work, but it only needs a little improvement.
quote: Original post by GravtyKlz
strcpy has to be used to copy an array but in thatcase I dont think it will let you copy a single character of the array. I think when you did out[j] = in; would mean that that one character of the string out would be set to the entire string of in rather than the right character. I did something about what you did except I just used one variable and it works fine.
Sorry, you''re wrong. You haven''t been programming for very long, have you? I don''t mean to offend you, but be more careful when you dispense advice.
quote: Original post by Roble
How would I go about changing the inversed string into integers?
Look up atoi() in the MSDN.
-Mike
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement