Hello :) I am in Computing Science 160 and need to do a C++ assignment. I need a program that reads in user input of strings (ex. PTLPPDNQL*) and stopping when a * is read. It then needs to read each letter as a value (P = pennie or 1 cent, T = toonie or 200 cents, etc). This is the part I need help with. Do I need to use an if or while statement or something like that? I started with this, but not sure what to do now:

#include <iostream>
#include <iomanip>
using namespace std;

void getInput(int &ncoins, int &namount)
int P = 0;
int N = 0;
int D = 0;
int Q = 0;
int L = 0;
int T = 0;
char x;
cout << "Coin Count Program" << endl;
cout << "Enter the string of coins (terminated by a *): ";
cin >> x;
//This is where I'm stuck.... I can't break up the cin into something i can decipher. should it be something like a    while (x != "8")   and then a bunch of if statements to see which variable gets increased? please help
ncoins = (P + N + D + Q + L + T);
namount = (P + (N*5) + (D*10) + (Q*25) + (L*100) + (T*200));

void doCalculations(int namount, int &namountd, int &namountp)
namountd = (namount / 100);
namountp = (namount % 100);

void showOutput(int ncoins, int namountd, int namountp)
cout << "Total number of coins: " << ncoins << endl;
cout << "Total amount: $" << namountd << "." << setw(2) << setfill('0') <<namountp << endl;

int main()
int ncoins;
int namountd;
int namountp;

getInput(ncoins, namount);
doCalculations(ncoins,namountd, namountp);
showOutput(ncoins, namountd, namountp);
return 0;

Edited 3 Years Ago by happygeek: fixed formatting

Try to take input using getche() of conio.h
it'll help u....
if not tell me........

The best way might be to use a while loop, eg:

char x;
string input;

cout<<"Please input the coin selection, or to terminate input a *: ";
if (x!='*')
while (x!='*');

<< moderator edit: added code tags: [co[u][/u]de][/co[u][/u]de] >>

now you have a string of inputs, and you can iterate through them one by one with a for loop and accessing it as an array, e.g.

for (int i=0; i<(int)input.length(); i++)
if (input[i]=='P') 
//do stuff
else if //rest of prerequisites

Hope it helps.


Yar it is so easy i don't know y u r not understanding it.
Now Listen

#include<conio.h>// for getche();

char ch;
char input[10];

int index = 0;

ch = getche();
while(ch != '*'){
input[index] = ch;

// Now u've got input in input n you can use it as u like O.K
// Do Tell me if it did n't work

This article has been dead for over six months. Start a new discussion instead.