0
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    double meal_one;
    double tip;
    double tip_dol;
    double total_meal;
    double calc_tip;
    double expen;


    while(meal_one>=0)
        {
            cout << "Enter cost of meal: $";
                cin >>meal_one;

    if(meal_one>=0)
        {
            cout <<"Enter tip percentage: ";
            cin>>tip;cout<<"percent"<<endl;
                tip_dol=(tip/100)*meal_one;
            cout << fixed <<setprecision(2)<< "Calculated Tip = $"<< tip_dol<<endl;
                expen=meal_one+tip_dol;
            cout<< fixed <<setprecision(2)<<"Total expenditure = $"<< expen << endl;
            cout<<endl;
        }
    else{
            cout << "Thank you for visiting the Zatoichi Sushi Hut!"<<endl;
        }
        }
    return 0;
}

I need to write code so that when the user enters the tip amount, the system will assign a % symbol right after it. I have included my code the line that i am asking about is the first two right after the if statement.

Thank you!

Lucas Brawdy
  • 43
  • 2
  • 8
  • I think asking for the person to enter the tip amount as a percentage is not fair – Ed Heal Oct 03 '15 at 14:11
  • To be clear, you are wanting it so that when the user types in "20", it actually shows up as "20%" instead? – Vaughn Cato Oct 03 '15 at 14:15
  • I'm sorry, what? Where should the percentage sign appear? Why are you not just using `'%'`? – Shoe Oct 03 '15 at 14:15
  • Do you want to enable users to enter "%"? – Christian Hackl Oct 03 '15 at 14:17
  • When they enter the percent amount say 10 I want the code to read 10% but currently it is going to a new line after 10 and the % appears on the next line – Lucas Brawdy Oct 03 '15 at 14:17
  • Currently, no "%" can appear at all, only "percent". – Christian Hackl Oct 03 '15 at 14:20
  • 1
    I guess @melak47 has finally understood what you are trying to tell us. The bad news is that this is completely impossible with standard C++. You'd have to use a 3rd-party library. Why would you "need" to write code to do that? – Christian Hackl Oct 03 '15 at 14:22
  • 2
    How you do this depends on the terminal emulator your program is running in. There are various libraries that can help you like [ncurses](https://www.gnu.org/software/ncurses/). – Galik Oct 03 '15 at 14:22
  • so from what im reading, since im running it and pressing enter to move to the next command after the cin of entering the percent, me hitting enter is causing the % sign to be moved to the next line? – Lucas Brawdy Oct 03 '15 at 14:34
  • Correct and with bog-standard console input there's nothing you can do about that. Even if you required users to disable line buffering, how would you signal end-of-input? What character would you require your users to press to end the percentage input? This is just nonsense overall :) – Lightness Races in Orbit Oct 03 '15 at 15:23

4 Answers4

0

Just change:

cin>>tip;cout<<"percent"<<endl;

to:

cin>>tip;cout<<'%'<<endl;
Shoe
  • 74,840
  • 36
  • 166
  • 272
0

The output seen when entering data is what the default system input produces. It normally directly echos characters entered including a newline when enter is used.

If you really need to output something on the same line as the input, you'll need to change how the default input works. On a UNIX system you can do that using tcgetattr() and tcsetattr(). I think you'd need to put the console settings to be non-canonical and to not echo.

With doing so you'll need to deal with echoing characters yourself, though. That is, you'll need to write each character entered. You could do that by characterwise reading from std::cin and writing characters as appropriate to std::cout. You'll need to collect the characters in a string to convert them to a value, too.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

You could probably hack it using getch() or getche() (on Windows) or some similar implementation on linux. This code just describes a general idea: you catch symbols that user types and rewrite your string again and again using \r special character (ASCII code for carriage return, read more here). You even can forbid non-numerical symbols!

This approach isn't very good because you have to invent a wheel instead of using an existing solution but probably it still can be used as a quick fix for your problem.

#include <stdio.h>
#include <conio.h>

int main(void) {
  // You'll certainly have to modify it =)
  char c1, c2;
  printf("Input:  %%");
  c1 = getch();
  printf("\rInput: %c%%", c1);
  c2 = getch();
  printf("\rInput:%c%c%%", c1, c2);
}
Community
  • 1
  • 1
mikrut
  • 1
  • 1
-1
void input(double &tip){
  char A[10];
  cin>>A;

  int bd = 0,i = 0;
  int ad = 0;
  double di = 1;

  for(i = 0; (A[i] != '.') && (A[i]!='%') && (A[i] != '\0');i++){
      bd = bd*10 + (57-A[i]);
  }

  for(i += 1; (A[i]!='%') && (A[i] != '\0');i++){
      ad = ad*10 + (57-A[i]);
      di *= 10;
  }

  tip = bd + ad/di;
}

Using this function yoy can give the input as 23.12% or something like that. This function takes a string as input and then extracting the value to be assigned to tip variable.

cryptomanic
  • 5,986
  • 3
  • 18
  • 30