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 6697335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:26:53+00:00 2026-05-26T06:26:53+00:00

#include <stdio.h> #include <iostream> using namespace std; float cost, total; bool loop(char item){ switch

  • 0
#include <stdio.h>
#include <iostream>
using namespace std;

float cost, total;
bool loop(char item){
        switch (toupper(item)) {
            case 'A':
                cost = 4.25;        
                return true;
            case 'B':
                cost = 5.57;
                return true;
            case 'C':
                cost = 5.25;
                return true;
            case 'D':
                cost = 3.75;
                return true;
            case 'T':
                return false;
        }
        return true;
}

int main(){
        char item;
        do {
            printf("\nEnter Item Ordered [A/B/C/D] or T to calculate total:");
            scanf("%c", &item);
            total = total + cost;
        } while (loop(item)); 
        printf("Total Cost: $%f\n", total);
}

Let me output the process:

$ ./Case3.o 

Enter Item Ordered [A/B/C/D] or T to calculate total:a

Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:b

Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:a

Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:t
Total Cost: $28.139999

Why is it after the first printf its printing the printf twice but skipping me from input the first time. then how is it calculating 5.24+5.57+5.24 to equal 28.14?

  • 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-05-26T06:26:54+00:00Added an answer on May 26, 2026 at 6:26 am

    As others have mentioned, When you press Enter, two characters get inputted, the character you enter + the newline, You need to account for both of these.

    Possible solutions are:

    Approach 1: The C way

     scanf(" %c", &item);
           ^^^
    

    Add a space here, or the better approach,

    Approach 2: The C++ way

    simply use the C++ way of getting input from user.

    cin >> item;
    

    Why the result is Undefined?
    Because you did not initialize the variable total, This results in Undefined Behavior giving you unexpected output.
    total is a global so it will be Default Initialized to 0.0.
    Real reason for Undefined result is in @Mystical’s answer.

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

Sidebar

Related Questions

#include stdio.h #include conio.h #include <iostream> using namespace std; int main (void) { char
#include <stdio.h> #include <string.h> #include <conio.h> #include <iostream> using namespace std; char a[21]; //
#include <stdio.h> #include <iostream> using namespace std; int main(void) { bool premiereLignefaite = false;
#include <stdio.h> #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; void multiplier_matrice_vecteur (char
This works: #include <iostream> using namespace std; but this fails: #include <stdio> When is
I am using the following code: #include <stdio.h> #include <iostream> using namespace std; int
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
#include <stdlib.h> #include <stdio.h> #include <iostream> #include <string.h> using namespace std; int main() {
#include <winsock2.h> #include <windows.h> #include<stdio.h> #pragma comment(lib, Ws2_32.lib) #include <iostream> using namespace std; int
#include<iostream> #include<memory> #include<stdio> using namespace std; class YourClass { int y; public: YourClass(int x)

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.