LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-16-2003, 04:52 AM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
Problems with switch...


K i'm newish to programming - C

Finally got round pointers and the one thing I thought I could do was switches, but i get the following when I try to compile my little app...

a2.c: In function `question':
a2.c:16: switch quantity not an integer


this is the code....

Code:
#include <stdio.h>
 
get_data(text)
char *text;
{
  printf("Enter the statement: ");
  scanf(" %s", text);
}
 
question(blah)
int *blah;
{
printf("Please select 1 - 5: ");
scanf(" %d", blah);
 
  switch(blah) {
    case '1':
      printf("Yes 1 would be good if you enjoy being single");
      break;
    case '2':
      printf("2 is good :)");
      break;
    case '3':
      printf("Haha 3 would be god, WAKE UP");
      break;
    case '4':
      printf("Haha now dreams may cost you");
      break;
    case '5':
      exit(1);
  }
}
 
int main() {
  int amount, i;
  char text_statement[100];
  int blah_blah;
 
  get_data(&text_statement);
 
  printf("How many times would you like to print the statement :: ");
  scanf(" %d", &amount);
  
  question(&blah_blah);
 
  printf("\nPrinting statement %d times\n", amount);
  for(i = 1;i <= amount;i++)
    printf("%s\n", text_statement);
}

Last edited by AMMullan; 12-16-2003 at 04:58 AM.
 
Old 12-16-2003, 05:25 AM   #2
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Hi,
try using
switch(*blah) in place of switch(blah) and it should work

and it should be case 1: instead of case '1': etc.

Last edited by Kumar; 12-16-2003 at 05:27 AM.
 
Old 12-16-2003, 05:33 AM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Yeah it worked perfectly, thanks

hmmm weird point tho, when i run it it doesn't come print the statement thats in the switch... weird?

Also, why would the switch statement need a pointer in front of the variable?
 
Old 12-16-2003, 05:41 AM   #4
Tesl
Member
 
Registered: Jun 2003
Location: Durham, UK
Distribution: Slackware 9, Mandrake 9.1
Posts: 163

Rep: Reputation: 30
the * itself isnt a pointer, you have declared

int * blah;

which means that the variable blah is the pointer. The pointer points to the parameter. When you called that function instead of passing an integer, you passed a reference (ie you passed the address of where an integer is stored).

This means that in the original switch statement, it was trying to switch a reference, or an address, which it cant do. When you use the pointer, it tries to switch the data that the pointer is pointing to. The pointer is pointing at that address, so it takes its (integer) value, which can then be switched

I really hope that made some sense, im in a big rush right now =/

by the way though, it didnt even compile at all for me when i tried it oO
 
Old 12-16-2003, 05:47 AM   #5
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
hehe it compiled without any errors for me :-P

as for making sense, i got 99% of it, still reading and playing round with pointers so i'll get the rest eventually

thanks for the huge help guys
 
  


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
KVM Switch Problems aldaeron Linux - General 28 10-12-2007 11:15 AM
problems with mice/keyboard switch ilectrcbob Linux - Hardware 1 01-18-2005 07:21 AM
Problems with linux and a kvm switch... newlinuxguy4 Linux - General 4 07-18-2003 09:23 AM
2PC w/ Switch boot problems dsiguy Linux - General 1 12-30-2002 05:10 PM
Switch Box Problems Sathe Linux - General 0 12-27-2001 09:27 AM

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

All times are GMT -5. The time now is 06:57 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