PDA

View Full Version : C code question...



newbie69
05-26-01, 10:13 PM
i want to type a word out and show the ascii numbers of the word then add a 1 for the first letter then +2 for the second letter, then +3 for the third letter and so on for each letter with the ascii numbers:

*example*

p = 112 + 1 = 113
b = 98 + 2 = 100
e = 101 + 3 = 104
n = 110 + 4 = 114
o = 111 + 5 = 116
i = 105 + 6 = 111
t = 116 + 7 = 123

then this would display: 113100104114116111123

can anyone help me with the code?

Grimoire
05-27-01, 02:35 PM
Assuming your input string is char * pszInput, the code would be:

int i;
int len = strlen(pszInput);

for(i = 0; i < len; i++)
printf("%03d", pszInput[i] + i + 1);

newbie69
05-27-01, 09:52 PM
thanks for replying :) :

this is what i put in my turbo c compiler
_____________
#include <stdio.h>

void main()
{
int i;
int len = strlen(pszInput);

for(i = 0; i < len; i++)
printf("%03d", pszInput[i] + i + 1);
}
____________

it gave me 2 errors "undefined symbol pszinput in function main"
and "invalid indirection in function main"

i want to be like "please type in a word: "
and "this is the outcome: " something like that

btw im a newbie in c as you can tell, do you think you can post the code from beginning to end?

thanks in advance

Grimoire
05-28-01, 09:15 PM
The problem is that the pszInput variable is not defined. As I originally said, I assume that you have some input string called pszInput. You don't. From what I see, you either want to have to user to be prompted and then type in the string, or enter it as a command line parameter. I would opt for command line parameter, since that's a lot more fun! :D

Change the void main() { line to be:

int main(int argc, char *argv[]) {

argc will contain the number of command line parameters, and argv[] contains the parameters themselves (as char *).

So, you'll want to make sure that you have something entered on the command line. So, you will need something like:

if(argc < 2){
printf("Correct usage: filename.exe string");
return -1;
}

char * pszInput = argv[1];

At the very end of the program, you will want to return 0;

Depending on the compiler, the char * pszInput variable declaration and assignment may not work there. You may have to declare the variable at the start of the function, and then just do the assignment after the if statement.

As for posting the whole code from beginning to end, well, I can't do the WHOLE assignment for you, now can I? :D

But seriously, that should get you going, and it doesn't hurt to have to struggle with things a little bit.