Thread: Help!!! I need to get this done asap!! Please!!

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    2

    Help!!! I need to get this done asap!! Please!!

    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.Help!!! I need to get this done asap!! Please!!-1-png

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    2
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Sorry about that. This is my first post so I didn't know.
    Help!!! I need to get this done asap!! Please!!-announcements-png
    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.
    Code:
    $ 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 )
                              ^
    I've shown you
    - 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.

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    104
    Code:
    if( studentName == 555 )
    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".
    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]

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Dren View Post
    To do what you're trying to accomplish, you need to iterate [...]
    Umm, not really unless a school assignment asks you to do it that way. The normal way is to use strcmp()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help ASAP!!!
    By pmit in forum C Programming
    Replies: 9
    Last Post: 10-20-2015, 12:42 PM
  2. Need help!!!!!! Asap!!!!!
    By DMJJR1988 in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2010, 01:20 AM
  3. need help ASAP ,
    By Haytham in forum C Programming
    Replies: 3
    Last Post: 05-14-2007, 10:21 AM
  4. Need Help ASAP....
    By Maxwell in forum C++ Programming
    Replies: 16
    Last Post: 09-14-2005, 06:56 PM

Tags for this Thread