Hi. I'm new to C programming and as I was doing homework from the class, I ran into some problem and I can't fix it. Please help me I need this code to loop around but when user types 555 or whatever, I want the program to stop.
Hi. I'm new to C programming and as I was doing homework from the class, I ran into some problem and I can't fix it. Please help me I need this code to loop around but when user types 555 or whatever, I want the program to stop.
You should post your code between [code][/code] tags. Images do nothing if we need to copy/paste to try things, or tell you what you did wrong.
How many warnings / errors does your == 555 line generate? This should be a clue.
Look up what the strcmp() function does.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Sorry about that. This is my first post so I didn't know. The line doesn't generate any errors but after compile and run, and type in 555, the program keeps going on with other messages.
> Sorry about that. This is my first post so I didn't know.
Or didn't care.
You still didn't post your code.
> The line doesn't generate any errors
Your compiler is rubbish then, or poorly configured.
I've shown youCode:$ cat foo.c #include <stdio.h> int main(void) { char studentName[100]; while ( 1 ) { if ( studentName == 555 ) break; } return 0; } $ gcc foo.c foo.c: In function ‘main’: foo.c:6:26: warning: comparison between pointer and integer if ( studentName == 555 ) ^
- how to use code tags
- how comparing arrays with ints is bad
- about strcmp
That is all your "urgent" needs or deserves.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
As Salem said, you can't compare an array directly with another operand. And even if you could, you are using the wrong types. 555 is an int, you would need to use "555".Code:if( studentName == 555 )
To do what you're trying to accomplish, you need to iterate through each element of the array, StudentName, and compare if with '5'. e.g if StudentName[0] == '5'. You must check there is a
valid element present before accessing the array and not to go beyond the array limit, StudentName[99]