1
votes

I am trying to add a percent sign directly after a users input (so that the user doesn't have to type the percent symbol). When I try this, it either goes to the next line or doesn't work at all.

What I want: _%
// the blank is for the user's input.

Sorry if this is messy, I'm not sure how to add c++ here.

Here are some things that I have attempted:

// used a percent as a variable:
const char percent = '%';

cout << "Enter the tax rate: " << percent; // obviously here the percent 
symbol goes before the number. 

double taxRate = 0.0;
cin >> taxRate >> percent; // here I tried adding it into the cin after the cin.

cin >> taxRate >> '%'; // here I tried adding the char itself, but yet another failed attempt...

So, is it even possible to do what I am wanting?

2
It's not possible (or desirable). And the iostream library is not really intended for interactive use.user2100815
well idk, this is just what we are learning in school, so I am trying to make it user friendly for myself I guess...hannacreed
Don't waste your time - it can't be done.user2100815
You'll have to abandon cin and use something like the curses library. Neil Butterworth has the right of it. Unless it's required by the assignment, you're better off spending your time on something that will get you a better grade.user4581301

2 Answers

0
votes

It is definitely possible, however iostream does not really provide a proper interface to perform it. Typically achieving greater control over console io requires use of some platform-specific functions. On Windows with VS this could be done with _getch like this:

#include <iostream>
#include <string>
#include <conio.h>
#include <iso646.h>

int main()
{
    ::std::string accum{};
    bool loop{true};
    do
    {
        char const c{static_cast<char>(::_getch())};
        switch(c)
        {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            {
                //  TODO limit accumullated chars count...
                accum.push_back(c);
                ::std::cout << c << "%" "\b" << ::std::flush;
                break;
            }
            case 'q':
            {
                loop = false;
                accum.clear();
                break;
            }
            case '\r': // Enter pressed
            {
                //  TODO convert accumullated chars to number...
                ::std::cout << "\r" "Number set to " << accum << "%" "\r" "\n" << ::std::flush;
                accum.clear();
                break;
            }
            default: // Something else pressed.
            {
                loop = false;
                accum.clear();
                ::std::cout << "\r" "oops!!                              " "\r" << ::std::flush;
                break;
            }
        }
    }
    while(loop);
    ::std::cout << "done" << ::std::endl;
    return(0);
}
0
votes

I have been having the same prob but I found an alternative, it doesn't automatically put % sign but it can let you add the %sign right after the cin without messing up when you run the code :> this is my homework, hope it helps as an example: enter image description here and here's what the output looks like:enter image description here

//Program that computes the total amount of savings after being invested
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    char percent [1];
    float IR, IRp, TC, P, I, A;
    cout << "Investment Rate:" << setw(10) << left << "";
    cin>> IR >> percent;
    IRp = IR*.01;
    cout << "Times Compounded: " <<setw(10)<<""; //TC
    cin>> TC;
    cout<<"Principal:" << setw(13) << right << "$"; //P
    cin>> P;
    A = P*(pow(1 + (IRp/TC), TC));
    I=A-P;
    cout<<"Interest: " <<setw(15)<<"$  " <<fixed<<setprecision(2)<<I<<endl;
    cout<< "Amount in Savings:" <<setw(5)<<"$"<<fixed<<setprecision(2)<<A<<endl;

    return 0;