GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Nov-2004, 04:25
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road

problem with switch case


in making menu option using switch case when i should use getchar?
i make an option menu that ecpect interger from user but when i enter char or word
the program will print the menu and it become infinite loop. How to solve that problem ?
thank you
  #2  
Old 22-Nov-2004, 05:45
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by if13121
in making menu option using switch case when i should use getchar?
i make an option menu that ecpect interger from user but when i enter char or word
the program will print the menu and it become infinite loop. How to solve that problem ?
thank you

I think I know what you are talking about but the old give a fish or teach to fish so first I'll show you my fish.

fish one
fish two
fish three

I found these by grabbing some lures and using the mighty "search" as a pole. A good method is to look at your question and find the words that seem to be valid to you. Just plunk them in the search dialog. Lots of fish there (and them's good eatin') just swimming around.

Another good method is to troll the forum by setting the "show past 45 days" to "show since the beginning" and read the thread titles. I know there are some that talk about just what you are talking about.

GIDForums uses google to search this site and you get results quickly with the word+word+word method. If that doesn't work, post some of your code and describe your problem in a little more detail. (me, I like to 'cast' into the waters until I snag something) Sorry about all the fishing analogies but I missed trout season this year and have to wait until spring.

Seriously, you will get better help (from the smart people here instead of me) if you post your code and some example input.
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 22-Nov-2004, 19:30
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road
Quote:
Originally Posted by if13121
in making menu option using switch case when i should use getchar?
i make an option menu that ecpect interger from user but when i enter char or word
the program will print the menu and it become infinite loop. How to solve that problem ?
thank you

I know it is a simple standard question then sorry if i posted too many lines here but i'm just a newbie :-)
my problem is in the main file:
CPP / C++ / C Code:
#include "text.h"
extern address FirstAvail;
int main()
{	Tabtext Tab;
	int pilihan; 
	int Nbfound=0,i=0,quit=0;
	int j=1;
	int foundline[20];
	int k;
	Kata search;
  do{	
	printf("============MENU============\n");
	printf("1.Input Text\n");
	printf("2.View Text\n");
	printf("3.Count word\n");
	printf("4.Count letter\n");
	printf("5.Count Line\n");
	printf("6.Search word \n");
	printf("7.Keluar\n");
	printf("============================\n");
	printf("Input pilihan:\n");
	scanf("%d",&pilihan); 
		switch(pilihan){
			case 1: printf("Input text end it with # \n");
					GetText(Tab);
				break;
			case 2: PrintText(Tab);
				break;
			case 3: printf("Nb Kata  = %d\n",CountKata(Tab));
			        getch();
				break;
			case 4: printf("Nb huruf = %d\n",CountHuruf(Tab));
			        getch();
				break;
			case 5: printf("Nb baris = %d\n",CountLine(Tab));
				getch();
				break;
			case 6: printf("Input word:\n");
				scanf("%s",&(search.TKata));
				getLen(&search);
				SearchKata(search,Tab,foundline,&Nbfound);
				printf("\n found %d in: \n",Nbfound);
				while(foundline[i]!=-99){
				   if (foundline[i]==foundline[i+1]){
				   	k=i;
					while(foundline[k]==foundline[i+1]){
						j++;i++;
					}
					printf("%d word in line %d\n",j,foundline[i]);
					j=1;
				    }
				    else{if(foundline[k]!=foundline[i])
				    	printf(" 1 word in line %d\n",foundline[i]);
				    	i++;
				    }
				}
				break;
			case 7: printf("Exit\n");
				quit=1;
				break;	
			default: printf("unknown option\n");	
				 break;		
		}
		 
	}
   	while(quit==0);
	return 0;
}

header file:
CPP / C++ / C Code:
#include <stdio.h>
#ifndef text_H
#define text_H
#define Max 2000
typedef char Tabtext[Max];
typedef struct {
	char TKata[15];
	int len;
}Kata;

void GetText(Tabtext Tab);
int IsEQ( Kata T1, Kata T2);
int isSeparator(char CC);
void SearchKata(Kata KK,Tabtext Tab,int foundline[20],int * Nbfound);
int CountKata(Tabtext Tab);
void PrintText(Tabtext Tab);
void getLen(Kata * L);
int CountHuruf(Tabtext Tab);
int CountLine(Tabtext Tab);

#endif 

body function file
CPP / C++ / C Code:
/*File :text.c 
body prototype*/

#include "text.h"
#define esc 27

void GetText(Tabtext Tab)

{	char ch, tekan;
    	int x=0;
    	static int retval;
    	do
	{	retval = scanf("%c", &ch);
		if(ch=='#'){Tab[x]=' '; x++;} 
		Tab[x]= ch;
		x++;
	}
	while (ch != '#');
		
}

int IsEQ( Kata T1, Kata T2)

{     
	int i = 0;
	if (T1.len !=  T2.len) return(0);
	else{
		for (i=0;i < (T1.len);i++ ) {
		if   (T1.TKata[i] != T2.TKata[i]) 
		     return (0);
		}
		return(1);
       
            }
}  

int isSeparator(char CC)

{	return(CC==' '|| CC =='\n' || CC == '.'|| CC == ',' );
}

void SearchKata(Kata KK,Tabtext Tab,int foundline[20],int * Nbfound)

{	Kata tempKata;
	int i=0,x=0;
	int Line=0;
	while(Tab[x]!='#'){
		while (isSeparator(Tab[x])==1){
			if(Tab[x]=='\n') Line++;
			printf("%c",Tab[x]);
			x++;
		}
		while(! isSeparator(Tab[x]) && Tab[x]!='#'){	
			tempKata.TKata[i]=Tab[x];
			i++;x++;
		}
		tempKata.len=i;
		tempKata.TKata[tempKata.len]='\0';
		if(IsEQ(KK,tempKata)){
			(*Nbfound)++;
			foundline[*Nbfound-1]=Line;
			/*textcolor(13); cprintf("%s",tempKata.TKata)*/;
			printf("%s",tempKata.TKata);
		}
		else{
		printf("%s",tempKata.TKata);
		}
		tempKata.len=0; i=0;/*reset tempKata*/
	}
	foundline[*Nbfound]=-99;
}

int CountKata(Tabtext Tab)

{
	int x=0,NbKata=0;
	while(Tab[x]!='#'){
		while (!isSeparator(Tab[x]))
			x++;
		NbKata++;
	   	while(isSeparator(Tab[x]))x++;
	}
	return (NbKata-1);
}

int CountHuruf(Tabtext Tab)

{	int x=0,NbHuruf=0;
	while(Tab[x+1]!='#'){
	if(!isSeparator(Tab[x])){
			x++;
			NbHuruf++;
	    }
	    else x++;
	}
	return NbHuruf;
}

int CountLine(Tabtext Tab)

{	int x=0,NbLine=0;
	while(Tab[x+1]!='#'){
		if(Tab[x]=='\n') {
			x++;
			NbLine++;
		}
		else
		x++;
	}
	return NbLine;
}

void PrintText(Tabtext Tab)

{	int i=0;
	while (Tab[i+1] != '#')
	{printf("%c",Tab[i]);
	i++;}
	printf("\n");
	printf("Nb Kata  = %4d \n",CountKata(Tab));
	printf("Nb letter = %4d \n",CountHuruf(Tab));
	printf("Nb line = %4d \n",CountLine(Tab));
}

void getLen(Kata * L)

{	int i=0;
	while((*L).TKata[i] != '\0')
	{i++;}
	(*L).len = i;	  	
}


thank you
  #4  
Old 22-Nov-2004, 22:26
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road
i forget to delete (in text.h)
extern address FirstAvail;

another question
this is out of topic with previous question
why if changed
CPP / C++ / C Code:
int IsEQ( Kata T1, Kata T2)
{     
  int i = 0;
  if (T1.len !=  T2.len) return(0);
  else{
    for (i=0;i < (T1.len);i++ ) {
    if   (T1.TKata[i] != T2.TKata[i]) 
         return (0);
    }
    return(1);
       
            }
}
with this function my SearchKata function not work?
Whats wrong in this second function?
CPP / C++ / C Code:
int IsEQ( Kata T1, Kata T2)
{     
	int i = 0;
	int found = 1;
	if (T1.len !=  T2.len) {found = 0;}
	else
	{
		while (i < (T1.len) && found == 1)
	  {
		if   (T1.TKata[i] != T2.TKata[i]) 
		     {found = 0;}
		else {i++;}
		
	  }	           
        }
	return found;
}  
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 C Programming Language 2 25-Oct-2004 02:06
Card Game Help! Pravda C++ Forum 4 28-May-2004 09:40
there has to be a better way dabigmooish C++ Forum 8 17-May-2004 11:24
C++ file I/O CronoX C++ Forum 36 09-Mar-2004 18:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:05.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.