Read to a file without cin

No code to display, I just need to understand how to take inputs from a piece of paper and put them into my C++ code to print to an outfile without using infile or cin. I can only use if, else if, if else, and switch.

AS this may be simple for some, i'm very simple and need extra steps to understand things. Thanks for the time to explain.
What do you mean piece of paper??? (You could just use cstdio with stuff like fscanf())
Last edited on
Define the values in your source code. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>

int main ()
{
    // define constants to place values into the program
    // (take these values from the piece of paper)
    const double GBP_PER_USD = 0.811112 ; // conversion rate: US Dollars to Pound Sterling
    const int dollars = 1234567 ;
    const int cents = 89 ;

    // compute using the values
    const double usd_amount = dollars + cents / 100.0 ; // amount in US dollars
    const double gbp_amount = usd_amount * GBP_PER_USD ; // equivalent amount in pounds sterling

    // print results
    std::cout << std::fixed << std::setprecision(2) // print two digits after decimal point
              << usd_amount << " USD == " << gbp_amount << " GBP\n" ;
}
I mean literally a piece of paper. I have a program to write, per instructions from said piece of paper.

On this paper are my data inputs for test values to use. I DON'T know how to take this data and input it into my code for my formula to use it and spit out an answer on the outfile.

I'm very sorry, but I don't know the terminology that well and I have zero clue as to your sugguestion.
Would you PLEASE stop shut-gunning multiple topics on the same program? One topic will make it easier for us to help you. Even if/when you ask multiple questions.

http://www.cplusplus.com/forum/beginner/262229/
http://www.cplusplus.com/forum/beginner/262242/

Your required data shown in the first topic is real easy to input. You can either "hard-code" variables with the data in the source code, or get the input from the user when the program runs.
If you don't want to input values from the user or from file at run time then you have to set those values within your code before you compile and run it. This is what JLBorges has done for the three values GBP_PER_USD, dollars and cents. This is called "hard-coding" those values. The "const" is good practice, but isn't crucial.

There are other ways of giving initial values to something than writing "something = value": for example, the uniform initialisation that @Furry Guy alludes to in your other threads.

Your initial post was far from clear (have another look at it). Remember that we can't see either your "piece of paper" or your assignment.

There is another way of getting a few values to your code at run time, and that is "command line arguments". But, from your questions, I think you should probably do as JLBorges as done.
Last edited on
Here is the full assignment for this "Making Change" program.

Write a program to input two float values. The first float is the cost of some purchase made or the cost of goods and the seconds is the payment value towards the cost. You are to determine the change(if any) to return to the customer. You must also determine if they gave you enough money to cover the cost and if they didn't print out you are short on funds so pay more.

The change should be in terms of 20 dollar bills, and/or number of 10's, 5's, and 1's in bills.

Then the change for coins, the number of quarters, dimes, nickels, and pennies. DO NOT print out 0 for any value.

Print out the inputs, 'cost' and 'payment' and then the amount of change to return. Print results to a file to print and turn in.

Example output: purchase: 0.81, pay: 1.00 change: 1 dime, 1 nickel, 4 pennies.

Test data for your program:
Input: 1,2,3,4,5,6
Cost: 0.81, 65.00, 16.76, 22.00, 0.25, 42.05
Payment: 1.00, 100.00, 50.00, 20.00, 100.00, 42.05

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
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    ofstream out;
    out.open("MakeChange.out");

    float cost, payment, change;
    int total_pennies;
    const int TWENTIES = 2000, TENS = 1000, FIVES = 500, ONES = 100,
              QUARTER = 25, DIME = 10, NICKEL = 5,PENNY = 1;

    cout << "You've entered the Make Change Program\n\n\n";

    cout << "Enter the COST: ";
     cin >> cost;
    cout << "Enter the PAYMENT: ";
     cin >> payment;

     change = payment - cost;

    if(payment < cost)
    {
       cout << "You need more money to purchase this item.";
    }
        if(cost == payment)
       {
          cout << "No change.";
       }
     else
     {
        cout << "\nYour change is: $ " << change;

         total_pennies = static_cast<int>(change * 100);

        int num_twenties = total_pennies / TWENTIES;
            total_pennies %= TWENTIES;
            if(num_twenties)
            {
                cout << "\nTWENTIES: " << num_twenties;
            }

        int num_tens = total_pennies / TENS;
           total_pennies %= TENS;
             if(num_tens)
             {
                 cout << "\nTENS: " << num_tens;
             }

      int num_fives = total_pennies / FIVES;
          total_pennies %= FIVES;
            if(num_fives)
            {
              cout << "\nFIVES: " << num_fives;
            }

      int num_ones = total_pennies / ONES;
          total_pennies %= ONES;
            if(num_ones)
            {
               cout << "\nONES: " << num_ones;
            }

     int num_quarters = total_pennies / QUARTER;
         total_pennies %= QUARTER;
            if(num_quarters)
            {
              cout << "\nQUARTERS: " << num_quarters;
            }

     int num_dimes = total_pennies / DIME;
         total_pennies %= DIME;
            if(num_dimes)
            {
               cout << "\nDIMES: " << num_dimes;
            }

     int num_nickels = total_pennies / NICKEL;
         total_pennies %= NICKEL;
            if(num_nickels)
            {
               cout << "\nNICKELS: " << num_nickels;
            }

      int num_pennies =total_pennies / PENNY;
          total_pennies %= PENNY;
            if(num_pennies)
            {
               cout << "\nPENNIES: " << num_pennies;
            }
     }
    out.close();
    return 0;
}


I believe I have this program 95% completed. I'm just confused on the inputs still. I don't want to create all const variables, as it would force a lot more code.

Whenever one writes to a file, is there a way to have CIN and it print to the file?
to read from a piece of paper, you need 3 things:
1) a human
2) a scanner
3) OCR

have the human run the paper through the scanner. You read the stream/file/whatever result. Run the result thru the OCR (which is actually not too hard to write) and print out what you got.

I don't think this is really what you wanted. But it is what you asked.
Last edited on
Topic archived. No new replies allowed.