I use cin.get to pause the screen so i can display my data on the screen, cin.get() pauses it good on the first try. Basically if you hit "enter to continue" the loop works great with no jumping over the cin.get(). However, if you hit any other keys on the pause screen and then enter, the loop with jump over the cin.get() as many times you hit the other keys before you hit enter. Is there any other way to pause the screen with out using cin.get() or how do i correct this , below is my main() code. using switch statement for a menu. I need help on the main() function, if you have to i have pasted the other functions but don't have problems with them. I think i have an idea, maybe to flush the cin.get everytime before i hit cin.get() but how do I?? I have 4 cin.get() in my main(), there at the end of each case statement in the switch statement.
Help please????

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>


void instruct(void);
void classify(char cInput[], float fstats[], int i, int iOdd, int iEven, int iUpper,
		   	 int iLower, int iOaverage, int iEaverage);
void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
			 int iLower, int iOaverage, int iEaverage);
int  count(char cChoice, char cInput[]);
int  search(char cGet, char cInput[]);
void menu(void);


void main(void)
{
										
	int iOdd=1, iEven=2;
	int i=0, iCheck=0, j;
	int iUpper=3, iLower=4;
	int iOaverage =5, iEaverage=6;
	char cInput [60]  ;
	float fstats[7]={0,0,0,0,0,0,0};
	char cPick , cChoice, cGet;
	
cInput[i] = ‘\0’;
	menu();
	cPick = getche();

	for (;cPick != '6'; )
	{
		switch(cPick)
		{
			case '1':  system("cls");
			                instruct();
			                for (j=0; j<=7; j++)
				    fstats[j]=0;
			                classify(cInput,fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
			                 break;

	                                     case '2':  iCheck = strlen(cInput);
			                if (iCheck >1)
		                                 {
			                	system("cls");
				 for (j=0; j<=iCheck;j++)
					printf("%c", cInput[j]);
			                }
			                Else
			                {	
				   printf("\nThere is no string in the memory please input a string first\n");
			                     printf(“Press enter to continue to the menu);
			                }
			                cin.get();
			                 break;

	                                     case '3':  iCheck = strlen(cInput);
			                if (iCheck >1)
			               {
			              	  system("cls");
				  printf("Please input the character to search:");
			                    cGet = getche();
				   if (search(cGet,cInput) > 0)
			                             printf("\nthe character occurs first at the index %d\n",search(cGet,cInput));
				   else 
				           printf(“There are no occurrences of  that character, press enter to continue);
			                }
			              

      			                
                else
			                {	
				   printf("\nThere is no string in the memory please input a string first\n");
			                     printf(“Press enter to continue to the menu);
			                }
			                cin.get();
			                break;

	                                      

                                                          case '4':  iCheck = strlen(cInput);
			                   if (iCheck >1)
				 {
				      system("cls");
				       printf("please input character:");
			                        cChoice = getche();
				       if (count(cChoice, cInput) > 0)
                                                                                    printf("\nThere are %d occurences of the character %c\n", count(cChoice, cInput),                             
                                                                                         cChoice);
                                                                              else 
 				   	printf(“There are no occurrences of that character, press enter to continue); 
			                    }
				 else
			                   {	
				   printf("\nThere is no string in the memory please input a string first\n");
			                     printf(“Press enter to continue to the menu);
			                     }
				   cin.get();
				   break;

	                                          case '5':  iCheck = strlen(cInput);
			                     if (iCheck >0)
				   {
				         system("cls");
				         summary(fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
			                      }
			                     else
			                      {	
				         printf("\nThere is no string in the memory please input a string first\n");
			                           printf(“Press enter to continue to the menu);
			                      }

			                      cin.get();
			                       break;

		}
	system("cls");
	menu();
	cPick = getche();
	}
printf("\n\n");

}

void instruct(void)
{
	printf("To terminate the program, strike the spacebar or Enter key.\n\n\n");
	printf("Next key>");
}

void classify(char cInput[], float fstats[], int i, int iOdd, int iEven, int iUpper,
			 int iLower, int iOaverage, int iEaverage)
{
	int j =0, iResult =0;
	char cTemp;

	cTemp = getche();

	for(;(cTemp != 32 && j<=60 && cTemp != 13); )	//ascii 32 for blank and 13 for carriage return
	{
		if (isalpha(cTemp) != 0)
		{
		        if (isupper(cTemp) !=0)
	                              fstats[iUpper]++;
	                          else
		           fstats[iLower]++;
		}

		if(isdigit(cTemp) !=0)
		{
		          if( (cTemp%2) == 0)
		           {
		                   fstats[iEven]++;
			 iResult = cTemp - '0';
			 fstats[iEaverage] = (fstats[iEaverage] + iResult);
		            }
                                                else 
		            {
			  fstats[iOdd]++;
			   iResult = cInput[j] - '0';
			   fstats[iOaverage] = (fstats[iOaverage] + iResult);
                                                }
		}

		//if (cTemp == '\n')
			//fstats[i]--;

		cInput[j] = cTemp;
		cInput[j+1] = '\0';
		fstats[i]++;
		j++;
		cTemp = getche();
	}// for loop
}

void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
	      int iLower, int iOaverage, int iEaverage)
{
	if (fstats[iEven] ==0)
		fstats[iEaverage] =0;
	else 
		fstats[iEaverage]= fstats[iEaverage]/fstats[iEven];

	if (fstats[iOdd] ==0)
		fstats[iOaverage] =0;
	else
		fstats[iOaverage] = fstats[iOaverage]/fstats[iOdd];

	system("cls");

	printf("Thank you! Your summary:\n\n");
	printf("Total number of keystrokes entered:%.f\n", fstats[i]);
	printf("Upper case alphabetic characters:%.f\n", fstats[iUpper]);
	printf("Lower case alphabetic characters:%.f\n", fstats[iLower]);
	printf("Even digits:%.f\n", fstats[iEven]);
	printf("Odd digits:%.f\n", fstats[iOdd]);
	printf("Average value of the even digits entered:%.2f\n", fstats[iEaverage]);
	printf("Average value of the odd digits entered:%.2f\n", fstats[iOaverage]);

}

int count(char cChoice, char cInput[])
{
	int i, iNum, iTotal =0;

	iNum = strlen(cInput);
	for ( i=0; i<=iNum; i++)
	{
		if (cChoice == cInput[i])
			iTotal++;
	}

	return iTotal;
}


int search(char cGet, char cInput[])
{
	int i, iIndex=0, iNum=0;

	iNum = strlen(cInput);
	for (i=0; i <= iNum; i++)
	{
		if (cGet == cInput[i])
		{
		       iIndex = i;
		        break;
		}
	}

	if (iIndex >= 0)
	         iIndex++;
	         

	return iIndex;
}


void menu(void)
{
	printf("1.  Enter a string of characters\n");
	printf("2.  Display the current string\n");
	printf("3.  Search for a character\n");
	printf("4.  Count occurences of a specific character\n");
	printf("5.  Produce a summary report on the current string\n");
	printf("6.  Exit\n\n");

}

Recommended Answers

All 3 Replies

cin.get only gets one character. If there are more than one characters in the stream, the pause won't work the way you want. Try this instead of a direct call to get:

#include <limits.h>

void pause ( const char *msg = "" )
{
  cout<< msg;
  cin.ignore ( INT_MAX, '\n' );
  cin.get();
}

Of course, that's hackish compared to if you were using standard C++.

i kinda get it, basically, i have done some programming before with classes but this program is very basic without using pointers. is there is simple way to do it. Like can you clear the stream before you hit the cin.get()???

>Like can you clear the stream before you hit the cin.get()???
Um, what do you think the code I gave does? cin.ignore basically just hides a loop that does this:

char ch;

while ( cin.get ( ch ) && ch != '\n' )
  ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.