Switch Statement

Switch statement witch inpunt function

Page 1 of 1

7 Replies - 1316 Views - Last Post: 11 September 2009 - 08:02 PM Rate Topic: -----

#1 LiDoNg_9_0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 159
  • Joined: 03-September 09

Switch Statement

Post icon  Posted 03 September 2009 - 05:52 AM

#include<conio.h>
#include<iostream.h>

struct info
{
	char name[50];
	char address[50];
	int age;
	char gender;
	char course[30];
	char school[50];
}A;


int main()
{
   char ch;
   
   do{
		printf("Menu: [S] Set Value\t [O] Open\t[Q] Quit\n");
   ch = getch();
   switch(ch){
	   case 'S':
				   cout<<endl<<"Name: "; gets(A.name);
				   cout<<endl<<"Address: "; gets(A.address);
				   cout<<endl<<"Age: "; cin>>A.age;
				   cout<<endl<<"Gender [M/F]: "; A.gender=getche(); //I think this is the problem...


				   cout<<endl<<"Course: "; gets(A.course);
				   cout<<endl<<"School: "; gets(A.school);
				   break;
	   case 'O':
				   cout<<endl<<"Name: "<<A.name;
				   cout<<endl<<"Address: "<<A.address;
				   cout<<endl<<"Age: "<<A.age;
				   cout<<endl<<"Gender: "<<A.gender;
				   cout<<endl<<"Course: "<<A.course;
				   cout<<endl<<"School: "<<A.school;
				   break;
	   case 'Q': break;
	  default:
				  puts("Invalid Input");
   }
   }while(ch != 'Q');
  system("pause");
  return 0;
}


***added code tags -jjsaw5***

the problem is in the switch statement. It jump the input functions specially when i input character only....

Is This A Good Question/Topic? 0
  • +

Replies To: Switch Statement

#2 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: Switch Statement

Posted 03 September 2009 - 05:53 AM

:code:
Was This Post Helpful? 0
  • +
  • -

#3 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Switch Statement

Posted 03 September 2009 - 06:00 AM

Are you sure you entered S and not s?
Was This Post Helpful? 0
  • +
  • -

#4 dgirdhar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 13-June 08

Re: Switch Statement

Posted 03 September 2009 - 06:02 AM

There isno problem with switch, instead problem is with using gts function, use some other way to accept input.
Was This Post Helpful? 0
  • +
  • -

#5 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Switch Statement

Posted 03 September 2009 - 06:21 AM

#include<conio.h>
#include<iostream.h>
#include<ctype.h>
struct info
{
	char name[50];
	char address[50];
	int age;
	char gender;
	char course[30];
	char school[50];
}A;


int main()
{
   char ch;char G;

   do{
		printf("Menu: [S] Set Value\t [O] Open\t[Q] Quit\n");
   ch = getch();
   switch(toupper(ch)){
	   case 'S':
				   cout<<endl<<"Name: "; cin>>(A.name);
				   cout<<endl<<"Address: "; cin>>(A.address);
				   cout<<endl<<"Age: "; cin>>A.age;
				   cout<<endl<<"Gender [M/F]: ";cin>>G; //I think this is the problem...
				   if(G == 'M' || G == 'F') A.gender = G;
				   cout<<endl<<"Course: "; cin>>(A.course);
				   cout<<endl<<"School: "; cin>>(A.school);
				   break;
	   case 'O':
				   cout<<endl<<"Name: "<<A.name;
				   cout<<endl<<"Address: "<<A.address;
				   cout<<endl<<"Age: "<<A.age;
				   cout<<endl<<"Gender: "<<A.gender;
				   cout<<endl<<"Course: "<<A.course;
				   cout<<endl<<"School: "<<A.school;
				   break;
	   case 'Q': break;
	  default:
				  puts("Invalid Input");
   }
   }while(ch != 'Q' || ch != 'q');
  system("pause");
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 LiDoNg_9_0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 159
  • Joined: 03-September 09

Re: Switch Statement

Posted 07 September 2009 - 07:51 PM

View Postjjsaw5, on 3 Sep, 2009 - 04:53 AM, said:

:code:

sory for that... tanx!

thanks a lot guyz. il just try to run it out...
Was This Post Helpful? 0
  • +
  • -

#7 LiDoNg_9_0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 159
  • Joined: 03-September 09

Re: Switch Statement

Posted 11 September 2009 - 07:59 PM

cin functions does not read white space. thats y i use the gets function. is there any input function except from gets that includes the spaces?
Was This Post Helpful? 0
  • +
  • -

#8 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Switch Statement

Posted 11 September 2009 - 08:02 PM

C++ has the getline(cin, STRING NAME); function that read an entire line spaces and all and stores it into a C++ string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1