Handyman
04-14-2003, 05:57 PM
Yep, this is something I probably should have learned long ago, but alas the problem hasn't reared it's ugly head until now :) I have the following code:
#include <stdio.h>
#include <string.h>
#include "stack.h"
#include "mcsfcn.h"
int main(void)
{
char* temp_eqn;
// Prompt for and read in equation from user //
printf("MCS Reverse Polish Notation Calculator\n\n");
printf("Please enter your equation: ");
scanf("%s",&temp_eqn);
printf("\n\n");
for ( int i = 0 ; i < length_of(temp_eqn) ; i++ )
printf("%c ",temp_eqn[i]);
return 0;
}
Nothing in the stack.h file has been called yet, but the mcsfcn.h contains the length_of() function I used in my for loop. The function is declared as follows.
int length_of(char* x)
{
for ( int i = 0 ; i != '/0' ; i++ );
return i;
};
So my problem occurs after the user has entered their string and the for loop is entered. It's supposed to just spit the string back out at me (printed char by char in the loop). However, all I get is about 20 lines of random ASCII characters and then the program ends.
So where abouts do I seem to be going wrong? Is my Length_of() function to blame? Since the loops seems to last for far longer than it should. Or is it in how I'm accessing my character array? Any help would be much appriciated :)
PS: I'm well known for making the simpliest/dumbest mistakes, so feel free to laugh at my inferior programming arse :) hehe
Thanks in advance!!
#include <stdio.h>
#include <string.h>
#include "stack.h"
#include "mcsfcn.h"
int main(void)
{
char* temp_eqn;
// Prompt for and read in equation from user //
printf("MCS Reverse Polish Notation Calculator\n\n");
printf("Please enter your equation: ");
scanf("%s",&temp_eqn);
printf("\n\n");
for ( int i = 0 ; i < length_of(temp_eqn) ; i++ )
printf("%c ",temp_eqn[i]);
return 0;
}
Nothing in the stack.h file has been called yet, but the mcsfcn.h contains the length_of() function I used in my for loop. The function is declared as follows.
int length_of(char* x)
{
for ( int i = 0 ; i != '/0' ; i++ );
return i;
};
So my problem occurs after the user has entered their string and the for loop is entered. It's supposed to just spit the string back out at me (printed char by char in the loop). However, all I get is about 20 lines of random ASCII characters and then the program ends.
So where abouts do I seem to be going wrong? Is my Length_of() function to blame? Since the loops seems to last for far longer than it should. Or is it in how I'm accessing my character array? Any help would be much appriciated :)
PS: I'm well known for making the simpliest/dumbest mistakes, so feel free to laugh at my inferior programming arse :) hehe
Thanks in advance!!