Simple Survey

Hi,,

I've been asked to do a simple C survey. An Open loop is running till "Q" is entered then loop will end and program will print out the results.

The survey is simple.. two questions.

1- Do you have access to the Internet from home
2- Do you use email from home?

if the answer on the first question is YES then the second question should be asked, otherwise the loop should start again

when ending the loop by pressing Q .. a statistic showed appear showing how many users were asked. How many use the net at home, and how many use emails at home.


the code looks like this

=================


void main ()

{
      int acc=0, email=0, num=1;
      int ans=0, ans1=0;


      printf("\tthis is a simple survey program\n\n");
      printf("Press -1 to end data entry.\n\n");

      printf("\tThis is interviewer #: %d", num);
      printf("\n\nDo you have access to the Internet from home?");
      printf("\nEnter Y for yes or N for no\n");
      scanf("%d", &ans);

      while (ans!= -1)
      {
            if (ans==1)
            {
                  acc++;
                  printf("\nDo you use email from home?");
                  printf("\nEnter Y for yes or N for no");
                  scanf("%d", &ans1);
                     if (ans1==1)
                     {
                      email++;
                      num++;
                      printf("\tThis is interviewer #: %d", num);
                      printf("\n\nDo you have access to the Internet from home?");
                      printf("\nEnter Y for yes or N for no\n");
                      scanf("%d", &ans);
                    }
                  else
                  {
                  num++;
                  printf("\tThis is interviewer #: %d", num);
                  printf("\n\nDo you have access to the Internet from home?");
                  printf("\nEnter Y for yes or N for no\n");
                  scanf("%d", &ans);
                  }
            }
            else
            {
            num++;
            printf("\tThis is interviewer #: %d", num);
            printf("\n\nDo you have access to the Internet from home?");
            printf("\nEnter Y for yes or N for no\n");
            scanf("%d", &ans);
            }

      }
      printf("\n\nOf %d interviewers:", num);
      printf("\n%d have access to the Internet", acc);
      printf("\n%d use email from home", email);

}

===================

this code works fine as it is.. but the checking variables ans & ans1 should be char type.. when i change them to that, the program doesnt work.

this is the code after changing them

====================

#include <stdio.h>

void main ()

{
      int acc=0, email=0, num=1;
      char ans, ans1;


      printf("\tthis is a simple survey program\n\n");
      printf("Press Q to end data entry.\n\n");

      printf("\tThis is interviewer #: %d", num);
      printf("\n\nDo you have access to the Internet from home?");
      printf("\nEnter Y for yes or N for no\n");
      scanf("%c", &ans);

      while (ans!= 'Q')
      {
            if (ans=='N')
            {
                  acc++;
                  printf("\nDo you use email from home?");
                  printf("\nEnter Y for yes or N for no");
                  scanf("%c", &ans1);
                     if (ans1=='N')
                     {
                      email++;
                      num++;
                      printf("\tThis is interviewer #: %d", num);
                      printf("\n\nDo you have access to the Internet from home?");
                      printf("\nEnter Y for yes or N for no\n");
                      scanf("%c", &ans);
                    }
                  else
                  {
                  num++;
                  printf("\tThis is interviewer #: %d", num);
                  printf("\n\nDo you have access to the Internet from home?");
                  printf("\nEnter Y for yes or N for no\n");
                  scanf("%c", &ans);
                  }
            }
            else
            {
            num++;
            printf("\tThis is interviewer #: %d", num);
            printf("\n\nDo you have access to the Internet from home?");
            printf("\nEnter Y for yes or N for no\n");
            scanf("%c", &ans);
            }

      }
      printf("\n\nOf %d interviewers:", num);
      printf("\n%d have access to the Internet", acc);
      printf("\n%d use email from home", email);

}

==============================

I need the code to work using N or Y

SouledAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

efnCommented:
The problem is that the user has to press Enter after entering a character to get the system to recognize the input, and the Enter key press generates a newline character in the input stream.  So the second scanf gets the newline character, rather than whatever the user entered in response to the prompt.

There are multiple ways to fix it.  One is just to read the newline character and throw it away.

char junk;

scanf("%c", &ans);
/* Consume the newline character from the input stream. */
scanf("%c", &junk);

This only works if the user enters exactly one character before pressing Enter.

A more resilient approach is to read a whole line at a time and only look at the first character.

char answer[1000];
fgets(answer, 1000, stdin);
ans = answer[0];

--efn
0
dennis_georgeCommented:
use getche(...) instead of scanf
0
efnCommented:
You can use getche if your compiler's library provides it, but you should be aware that it is not in the standard C library, and so may not be portable.  Of course, you may or may not care about portability.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

rstaveleyCommented:
As I recall, scanf will clobber the word at the address pointed to by the character address. It treats the address as if it was pointing to an int. Try reading it into an int temporary and then assigning the char to the value read into that tempotrary.
0
efnCommented:
> As I recall, scanf will clobber the word at the address pointed to by the character address.

If it does that with %c, the library implementation is defective.  With %lc, however, scanf expects to store a wide character (wchar_t).
0
avizitCommented:
there is one more way
from http://webcourse.cs.technion.ac.il/234117/Spring2004/ar/faq_Homework.html

I'm having a problem using scanf. After I enter the username, the following scanf reads the "enter" I clicked. What should I do?

Instead of writing scanf("%c") you can write scanf(" %c") (with a space before %c), and it should work better. The space before the "%c" will read all white spaces (newline, tab and space) and the first non-white space will be read into the "%c". Using the scanf this way will prevent the scanf from reading the "enter".
            
      
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rstaveleyCommented:
efn, you are right of course... and here's nothing quite as bad as writing folklore on the basis of the ideosynracies of a library in one's disinformative years :-)
0
efnCommented:
Oh, I think there are many things worse.  Don't get me started.  :-)  And I like that word "disinformative."
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.