You are here:

C++/help me plz

Advertisement


Question
Hi

can you please help me with this code below,i want this to put the returning value of the token function in x then ask if we want to continue the loop or not,but it seems that the scanf doesn't work,can you help me plz?

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

int token(char);

void main(){

  int f=0,d=0;

  char a=0,x=0;

  while(f!=1){
       
      x=token(a);

     printf("%c\n",x);

     printf("Do you want to continue?(Y/N)\n");
     

     scanf("%d",&d);//******My problem******
     printf("d=%d\n",d);

     if(d=='Y')
        f=0;

     else if(d=='N')
         f=1;
  }

  
}

int token(char a){


  printf("Enter:\n");

  scanf("%c",&a);

  printf("a=%c\n",a);

  return a;

}



Thanx
Bita

Answer
main *must* return an int. the int returned by main() is a way for a program to return a value to "the system" that invokes it. on systems that doesn't provide such a facility, the return value is ignored, but that doesn't make "void main()" legal C or C++. even if your compiler accepts "void main()", avoid it.

to read a single char using scanf, use %c and not %f.

scanf is a complicated beast, and should not be the first choice for someone just starting out programming in C.

in particular, reading a single character with %c rarely works as a beginner would expect. when you enter text you press the [Enter] or [Return] key and the newline char remains in the input buffer. scanf with %c reads any character that's available including white space characters (like newline). the need to flush the input buffer is a nasty issue that comes up (there is no foolproof and standard way to do this) when using scanf to read a char.

using fgetc to read a single character is easier. here is an example of a function that reads a single char 'y' or 'n' (as the first char in a line) using fgetc .

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

/* returns 1 for 'y', 0 for 'n' */
int get_y_or_n_answer( const char *question )
{
      fputs( question, stdout ) ;

      while(1)
      {
         int c, answer;

         /* write a space to separate answer from question. */
         fputc (' ', stdout);

         /* read the first character of the line.
         this should be the answer character, but might not be. */
         c = tolower( fgetc(stdin) ) ;
         answer = c;

         /* discard rest of input line. */
         while( c != '\n' && c != EOF ) c = fgetc(stdin) ;

         /* return if the answer was valid. */
         if (answer == 'y') return 1;
         if (answer == 'n') return 0;

         /* answer was invalid: ask again for valid answer. */
         fputs ("Please answer y or n:", stdout);
        }
}


int main()
{
      int yes_or_no = get_y_or_n_answer( "Do you want to continue?(Y/N)" ) ;

      if( yes_or_no ==  1 ) fputs( "yes, you do wnat to continue\n", stdout ) ;
      else fputs( "ok, bye then\n", stdout ) ;

      return 0 ;
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.