how to validate numeric input only using cin

So I am having trouble with this program it works but I need to input 2 values from the same line separated by a space using cin double chevrons!? I cannot use stringstream have to use some form of a char* token[] function using atof without having the program skip the second input if a non numeric is entered such as ten instead of 10, also need to have input read as a floating point or double int?! please correct or help me this is the code I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <iomanip>
using std::setprecision;

int main()
{
// Beginning information on lab and programmer

  cout << endl;
  cout << "Lab: 2" << endl;
  cout << "Programmer: Joshua Henry" << endl;
  cout << endl;

// Declare variables for cost, pay and change as well as double int
// for declaring change total with decimal point

  double pur = 0;
  double tend = 0;
  int change = 0;
  double balAmt = 0.0;

// constants to define amount for each bill and coin
  const int FIFTY = 5000;
  const int TWENTY = 2000;
  const int TEN = 1000;
  const int FIVE = 500;
  const int ONE = 100;
  const int FIFTYCENT = 50;
  const int QUARTER = 25;
  const int DIME = 10;
  const int NICKEL = 5;
  const int PENNY = 1;

// ints for the amount of bills in change  
  int fiftyBill = 0;
  int twentyBill = 0;
  int tenBill = 0;
  int fiveBill = 0;
  int oneBill = 0;
  int fiftyCent = 0;
  int quarterCent = 0;
  int dimeCent = 0;
  int nickelCent = 0;
  int pennyCent = 0;

// Ask the user to input both purchase and tendered amount

  cout << "Please enter the Purchase amount and amount tendered(seperated by a space): \n";
  cin >> pur >> tend;
  cin.ignore(0, ' ');

// converts purchase amount and tendered into int to solve  
  int cost = pur * 100;
  int pay = tend * 100;
 
// make operations to determine how many bills the change entitles
  change = pay - cost;
 
// while loop to get exact change in bills and coins till change due is zero
  while (change > 0)
  {
    if (change >= 5000)
    { 
      fiftyBill = change / FIFTY;
      change = change - (FIFTY * fiftyBill); 
    } 
      else if (change >= 2000)
      {
        twentyBill = change / TWENTY;
        change = change - (TWENTY * twentyBill);
      }
      else if (change >= 1000)
      {
        tenBill = change / TEN;
        change = change - (TEN * tenBill);
      }
      else if (change >= 500)
      { 
        fiveBill = change / FIVE;
   	change = change - (FIVE * fiveBill);
      }
      else if (change >= 100)
      {
	oneBill = change / ONE;
	change = change - (ONE * oneBill);
      }
      else if (change >= 50)
      {
	fiftyCent = change / FIFTYCENT;
 	change = change - (FIFTYCENT * fiftyCent);
      }
      else if (change >= 25)
      {
	quarterCent = change / QUARTER;
 	change = change - (QUARTER * quarterCent);
      }
      else if (change >= 10)
      { 
	dimeCent = change / DIME;
 	change = change - (DIME * dimeCent);
      }
      else if (change >= 5)
      { 
	nickelCent = change / NICKEL;
    	change = change - (NICKEL * nickelCent);
      }
      else if (change >= 1)
      {
 	pennyCent = change / PENNY;
	change = change - (PENNY * pennyCent);
      }
    
    }

// calculates the amount due with setprecision and showpoint

  balAmt = tend - pur;
  cout.setf(ios::fixed|ios::showpoint);
  cout << setprecision(2);
  cout << endl;

// output the amount of purchase, tendered and change due  
  cout << "For a purchase of " << pur << " the amount tendered is " << tend << endl;
  cout << "Your change will be $" << balAmt << endl;
  cout << endl;

// if statements to determine whether or not to output bill and coin values
  if (fiftyBill != 0 && fiftyBill == 1)
    cout << fiftyBill << " $50 bill" << endl;
  if (fiftyBill > 1)
    cout << fiftyBill << " $50 bills" << endl;
  
  if (twentyBill != 0 && twentyBill == 1)
    cout << twentyBill << " $20 bill" << endl;
  if (twentyBill > 1)
    cout << twentyBill << " $20 bills" << endl;

  if (tenBill != 0 && tenBill == 1)
    cout << tenBill << " $10 bill" << endl;
  if (tenBill > 1)
    cout << tenBill << " $10 bills" << endl;

  if (fiveBill != 0 && fiveBill == 1)
    cout << fiveBill << " $5 bill" << endl;
  if (fiveBill > 1)
    cout << fiveBill << " $5 bills" << endl;

  if (oneBill != 0 && oneBill == 1)
    cout << oneBill << " $1 bill" << endl;
  if (oneBill > 1)
    cout << oneBill << " $1 bills" << endl;

  if (fiftyCent != 0 && fiftyCent == 1)
    cout << fiftyCent << " 50-cent coin" << endl;
  if (fiftyCent > 1)
    cout << fiftyCent << " 50-cent coins" << endl;

  if (quarterCent != 0 && quarterCent == 1)
    cout << quarterCent << " 25-cent coin" << endl;
  if (quarterCent > 1)
    cout << quarterCent << " 25-cent coins" << endl;

  if (dimeCent != 0 && dimeCent == 1)
    cout << dimeCent << " 10-cent coin" << endl;
  if (dimeCent > 1)
    cout << dimeCent << " 10-cent coins" << endl;

  if (nickelCent != 0 && nickelCent ==1)
    cout << nickelCent << " 5-cent coin" << endl;
  if (nickelCent > 1)
    cout << nickelCent << " 5-cent coins" << endl;

  if (pennyCent != 0 && pennyCent == 1) 
    cout << pennyCent << " 1-cent coin" << endl;
  if (pennyCent > 1)
    cout << pennyCent << " 1-cent coins" << endl;
   
    cout << endl;

}
Last edited on
I need to input 2 values from the same line
I cannot use stringstream have to use some form of a char* token[] function using atof

That sounds a bit like a reference to strtok()
http://www.cplusplus.com/reference/cstring/strtok/

There are several ways to tackle this. One is to get the entire line as a single character string, and then parse it to get the two numbers. Another would be to get the two parts as two separate strings, as here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <iomanip>
using std::setprecision;

#include <cstdlib>

int main()
{
// Declare variables for cost, pay and change as well as double int
// for declaring change total with decimal point

  double pur = 0;
  double tend = 0;


  char cpur[50];
  char ctend[50];

// Ask the user to input both purchase and tendered amount

  cout << "Please enter the Purchase amount and amount tendered(separated by a space): \n";
  cin >> cpur >> ctend;

  pur = atof(cpur);
  tend = atof(ctend);

// display the input as a string, them as a number

    cout << "cpur  = " << cpur << endl;
    cout << "ctend = " << ctend << endl;
    cout << "\n---------------------\n\n";

    cout << "pur  = " << pur << endl;
    cout << "tend = " << tend << endl;

    return 0;
}


Normally I'd use the C++ std::string for this purpose, not sure whether that is allowed here.

ya I used that but he said he wanted the double chevron usage in this program very anal and not letting me get credit till I do it the "way" he taught in class but thanks Ill try and that
Chervil your the man! or woman! appreciate it hopefully it will pass inspection this time!
Topic archived. No new replies allowed.