[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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

0
Souled
Asked:
Souled
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now