Thread: Help needed.

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    Help needed.

    Why cant i get this code to work?
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char work;
    	printf("Hello, does this program work?\n");
    		work=getchar();
    		if (work != 'yes')
    		printf("That's too bad... ;)\n");
    	else
    		printf("Works! :D");
    	system ("PAUSE");
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    because 'yes' is not one char
    work can store only 1 char 'y' for example...

    Havn't you got any warnings from the compiler?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It never ceases to amaze me how people can make a mess of indenting even the shortest of programs
    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.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    So what should i use instead of "char"?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Denisius
    So what should i use instead of "char"?
    It depends on what do you want to achieve. Describe your goal.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    My guess is he'd want to use a char* and replace getchar() with fgets...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    It's ok, i solved the problem.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And dont use
    Code:
    system ("PAUSE");
    use getchar()

    ssharish2005

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    18
    Why should'nt i use
    Code:
    system ("PAUSE");
    ?

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Denisius
    Why should'nt i use
    Code:
    system ("PAUSE");
    ?
    I it not portable.
    You have a portable solution that does the same. So why bother with the SYSTEM?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Also is insecure. Someone might feel like adding a pause.exe file to your folder which wipes out your hard drive. your system("pause") call will then execute this file instead of the pause command.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM