Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C Loop termination question

Posted on 2001-07-25
1
Medium Priority
?
233 Views
Last Modified: 2010-04-15
Can someone please reccommend a good way to terminate the following loop when the user hits enter twice?  Currently the only way to exit is to user ctrl+d.

#include <stdio.h>
#include <ctype.h>

#define YES 1
#define NO  0

int main(void)
{

/**********************
Variable Declarations
***********************/

char string[101];
int c,
countChars = 0,
countLines = 0,
newToken,
countTokens = 1,
countUpper = 0,
countLower = 0,
countDigits = 0,
countOther = 0,
charsPerLine = 0,
countBreak = 0;

printf("Counts the number of characters, lines and words\n");

/******************************
Start of infinite while loop
*******************************/

while ((c = getchar()) != EOF){

/************************************************************
Get the character from std input and perform character count
************************************************************/

 ++countChars;
 ++charsPerLine;

/*************************************************************
Check for now lines and increment the line counter and reset
the charsPerLine variable
**************************************************************/

  if (c == '\n'){
   ++countLines;
   charsPerLine = 0;
  }

/*************************************************************
Verify that chars per line does note excede 100
*************************************************************/

  if ( charsPerLine > 100){
   printf("ERROR - The maximum number of chars per line is 100\n\n");
   break;
  }

/*************************************************************
Verify that chars per line does note excede 100
*************************************************************/

  if (countLines > 100){
   printf("ERROR - The Total number of lines cannot excede 100\n\n");
   exit(-1);
  }


/*******************************
Calculate the number of tokens
*******************************/

  if (c == ' ' || c == '\n' || c == '\t'){
   newToken = YES;
  }
  else
   {
    if (newToken == YES){
     newToken = NO;
     ++countTokens;
    }
  }

/********************************************************
Calculate the number of lower and upper case characters
*********************************************************/

 if ((isupper(c)) != 0)
   ++countUpper;
 else if ((islower(c)) != 0)
   ++countLower;


/*******************************
Calculate the number of digits
********************************/

 if ((isdigit(c)) != 0)
  ++countDigits;

/******************************************
Calculate the number of special chracters
******************************************/

 if ((isdigit(c)) == 0 && (isalpha(c)) == 0)
  ++countOther;

/******************
end of while loop
*******************/


/***
gets(string);
printf("\n You entered %c %s and %d\n", c, string, strlen(string));
if ((c == '\n') && (strlen(string) == 0))
 break;***/
c = 026;
}

/*************************************************************************
Print the Output
***************************************************************************/

printf("Number of lines = %d\n", countLines);
printf("Number of tokens = %d\n", countTokens);
printf("Number of chars = %d\n", countChars);
printf("Number of upper-case chars = %d\n", countUpper);
printf("Number of lower-case chars = %d\n", countLower);
printf("Number of digits = %d\n", countDigits);
printf("Number of chars other than chars and digits = %d\n", countOther);
return (0);

/****************
end of main
*****************/

}
0
Comment
Question by:Dargie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
newmang earned 120 total points
ID: 6320264
Why not store the last character read in a variable and then when a new character is read check to see if both the new character and the last character are both newlines. If so then break; the loop. Something like this perhaps....

----------------------------------------------------------
char lastchar=0x00;
.
.
.
.
.
while ((c = getchar()) != EOF){

if(c=='\n' && lastchar=='\n')
      break;
lastchar=c;



Cheers - Gavin
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question