Kevin
Kevin

Reputation: 69

add break statement in while loop with getchar

i am a starter and i have written a code:

#include<stdio.h>
char c;
char a[100]={0};
int i;

int main(void)
{   
  while(1)
  {
    i=0;
    while(1){
      if((c=getchar())!='\n'){
        a[i]=c;
        i++;
      } else {
        break;
      }
    }
    printf("%s\n",a); 
  }
  return 0;
}

and now i have idea that i want to add a break statement that when i type q, the program will break. so my new program is:

#include<stdio.h>
char c;
char a[100]={0};
int i;

int main(void)
{   
  while(1)
  {
    i=0;
    while(1){
      if((c=getchar())!='\n'){
        a[i]=c;
        i++;
      } else {
        break;
      }
    }
    printf("%s\n",a);
    if(getchar()=='q')break;       /*i added this*/
  }
  return 0;
}

i know because the getchar() conflict the code is not correct, how can i do, to add the type q break command. thanks a lot.

Upvotes: 0

Views: 982

Answers (3)

Soren
Soren

Reputation: 14688

Unlikely to be what you actually want, but at least this will work

#include<stdio.h>

int main(void)
{
  char a[100]={0};
  int i = 0;
  do {
    int c = getchar();
    switch (c) {
       case EOF:
           /*EOF*/
           return -1;
       case '\n':
           a[i] = 0;
           printf("%s\n",a);
           i= 0;
           break;
       default:
           a[i++] =c;
           break;
  } while (c != 'q' || i > 1);
  return 0;
}

Upvotes: 1

autistic
autistic

Reputation: 15632

i am a starter and i have written a code

Uh oh! Here goes... Firstly, the odds of successfully learning the portable, standard-compliant C programming language are overwhelmingly low for anyone who tries to learn by unguided trial and error. There's a really nice book you can use as a guide, but you need to read it AND do the exercises as you stumble across them. That's K&R2E, if you're interested.


Anything resembling char c; c = getchar(); is erroneous because the narrowing conversion from int (which getchar returns) to char discards error handling information.

Any character values returned by getchar will be as unsigned char values converted to an int, so it'd technically be more correct (though still incorrect) to use that type.

Error values are returned as a negative integer such as EOF, not to be confused with character values, which is exactly what your code does wrong by discarding the error information.

getchar returns int, so store the value into an int, then test for errors, then, if you must, convert it down to an unsigned char.


and now i have idea that i want to add a break statement that when i type q, the program will break.

Following on from the previous discussion, such a requirement is superfluous, nonetheless easy to incorporate if you're handling errors correctly. Just insert an extra test to match against q into the error handling, and presto!

Nonetheless, the error handling most likely already solves this problem, as most OSes have a mechanism to close stdin, which would cause getchar to return EOF, thus triggering the error handling clause. This is typically achieved by pressing CTRL+d in Linux, or CTRL+z in Windows, for example.


In conclusion, providing you aren't already reading one, it seems a nice book about C isn't all that'd benefit you; a nice book about console scripting for your favourite Unix-like OS would also be highly beneficial.

Upvotes: 1

Milind Deore
Milind Deore

Reputation: 3063

Try this code:

#include <stdio.h>
#include <stdlib.h>

#define q 113  --> These values are coming from ASCII table.
#define Q 81 

int main(void)
{
    char ch = 0;
    do
    {
        ch = getchar();
        switch(ch)
        {
            case q:
                // your logic goes here
                exit(0);
            case Q:
                // your logic goes here
                exit(0);
            /* more cases can come here */
        }
    }
    while(1);
}

Upvotes: 0

Related Questions