LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2004, 03:07 AM   #1
Hamid Moradmand
LQ Newbie
 
Registered: May 2004
Location: Iran
Distribution: Redhat
Posts: 7

Rep: Reputation: 0
Question to become giddy in simple code


Hi there!
When I enter a certain number, I don't recieve suitable answer.
what's wrong in my code source?

Thanks for your help


Code source
-------------------------------------------------
#include <stdio.h>
int main()
{
int num;
printf("\nEnter a number between 1 to 10:");
scanf("%d",num);
#if num == 1
printf("\nyou insert %d",num);
#elif num == 2
printf("\nyou entered %d",num);
#else
printf("\nyou entered except 1&2\n");
#endif
return 0;
}


Output
--------------------------------------------------
[root@Shvback c]# ./test2

Enter a number betwen 1 to 10:1

you entered except 1&2
Segmentation fault

Last edited by Hamid Moradmand; 05-18-2004 at 03:08 AM.
 
Old 05-18-2004, 03:18 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Well, first of all, you're using preprocessor calls where you shouldn't. Second of all, you need to pass a pointer to num to scanf(). The code should look like this:

Code:
#include <stdio.h>

int main()
{
  int num;

  printf("\nEnter a number between 1 to 10:");
  scanf("%d",&num);
  if(num == 1)
    printf("\nyou insert %d",num);
  else if(num == 2)
    printf("\nyou entered %d",num);
  else
    printf("\nyou entered except 1&2\n");

  return 0;
}
note: for future reference, please post code inside [ code] and [ /code] tags for readability's sake.

Last edited by itsme86; 05-18-2004 at 03:25 AM.
 
Old 05-18-2004, 06:23 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
Or you could just use:

Code:
#include <stdio.h>

int main(int argc, char *argv[]) {

  int input;

  printf("Entrer a number between 1 and 10: ");
  scanf("%d", &input);

  printf("You entered: %d :-)\n", input);

return 0;
}
Just makes it easier than using loops unnecessarily :-)

Last edited by AMMullan; 05-18-2004 at 06:24 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple PHP code aznita Programming 1 06-19-2005 11:50 PM
Interpret this simple C code Chase_G Programming 4 04-29-2005 09:07 AM
how to giddy up downloads?!... breezewax Linux - Networking 2 09-24-2004 07:17 PM
whats wrong with this simple code?? laclac01 Programming 3 08-23-2004 03:52 PM
simple code *help a newbie out* alimharji Programming 7 03-14-2004 01:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration