Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:55:49+00:00 2026-06-18T03:55:49+00:00

Possible Duplicate: Program doesn’t wait for user input with scanf(%c,&yn); I’ve just started programming

  • 0

Possible Duplicate:
Program doesn’t wait for user input with scanf(“%c”,&yn);

I’ve just started programming in C, and something strange (which I didn’t encounter in C++) is popping up.

In my header file, I declare a variable (see onemoretime in code for .h file), whose value is later set using user input through scanf(). For some reason, the conditionals which use the variable wouldn’t work properly, either returning an infinitely repeating result, or just not doing anything. I set some breakpoints, and found that the first time onemoretime is called in the main() function, the value of onemoretime is, for some reason, set to \n.

This isn’t a value scanf() is supposed to recognize, but the input is treated like a new line (obviously). When I enter y, which should trigger the else if conditional and loop back to the beginning of main(), Xcode’s debugger simply says, error: 'y' is not a valid command.

Here’s my .h file:

#ifndef C1_Header_h
#define C1_Header_h

float num1, num2;
char op, onemoretime;

#endif

… And here’s the relevant code from the .c file:

#include <stdio.h>
#include <math.h>
#include "Header.h"

int main(int argc, const char * argv[])
{
    printf("Enter a two-number calculation below:\n");
    while (1)
    {
        scanf("%f%c%f", &num1, &op, &num2);

        // addition
        if (op == '+')
        {
            printf("%f\n", num1+num2);
            printf("\nDone. Would you like to solve another math problem (y/n)?\n");
            scanf("%c", &onemoretime);
            while (1)
            {
                if (onemoretime == 'n') 
                {
                    printf("Ok. Terminating program.");
                    return 0;
                }
                else if (onemoretime == 'y') 
                {
                    // loop back to start main function
                }
            }
        }
    }
        // rest of code, irrelevant here
}

If it’s a factor at all, hovering over the onemoretime variable in Xcode (showing its basic properties), every instance of the variable after the initial declaration in the header file shows the value to be \n.

What am I doing wrong? How can the value of onemoretime be set by the user, rather than being set by the program before the user can get to it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:55:50+00:00Added an answer on June 18, 2026 at 3:55 am

    [\n] isn’t a value scanf() is supposed to recognize, […]

    That’s true in general, but “The normal skip over white-space characters is suppressed in [the case of %c]” [link].

    To get op and onemoretime, I’d recommend using fgetc (link) rather than scanf, and handling the whitespace-skipping yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: strange output in comparision of float with float literal So the program
Possible Duplicate: Why doesn't getchar() wait for me to press enter? I continue to
Possible Duplicate: Strange floating-point behaviour in a Java program Why does JSP/JSTL division by
Possible Duplicate: scanf: %[^\n] skips the 2nd input but %[^\n] does not. why? Basically
Possible Duplicate: A simple program to CRUD node and node values of xml file
Possible Duplicate: C# - How to get Program Files (x86) on Windows Vista 64
Possible Duplicate: Can I run from command line program created by Eclipse? I am
Possible Duplicate: Sleep Less Than One Millisecond How can I make a program sleep
Possible Duplicate: Platform independent paths in Java I make a program but it save
Possible Duplicate: How do I find a segfault in my C++ program? I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.