Help needed.

This is a discussion on Help needed. within the C Programming forums, part of the General Programming Boards category; Why cant i get this code to work? Code: #include <stdio.h> int main() { char work; printf("Hello, does this program ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    because 'yes' is not one char
    work can store only 1 char 'y' for example...

    Havn't you got any warnings from the compiler?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,682
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21