functions must be called from main() ?

Pages: 12
program done.
Last edited on
that is when it goes haywire...

What does this mean? Did your computer crash? Did your house burn down?

Show the inputs you gave, what output you expected, what output you got, and why you think it is wrong.
Line 81: My compiler generates an error because inputData does not return a value. Line 39 does not use the return value, so you might as well make this a void function.

I ran your program, and received reasonable results.
1
2
3
4
5
6
7
8
9
10
    //Input Function
    inputData(balance, interestRate, monthlyPaymentPercent);

    cout << "Balance=" << balance << endl;
    //Calculation
    interestRate = interestRate/100;
    cout << "Interest rate: " << interestRate << endl;
    
    interestPaidMonth = balance * interestRate;
    cout << "Interest per month: " << interestPaidMonth << endl;
Enter the credit card balance: $
1000
Enter the interest rate on the credit card (compounded monthly) :
1
Enter the desired monthly payment : $
100
Balance=1000.00
Interest rate: 0.01
Interest per month: 10.00
Press any key to continue . . .

I did modify your couts to add labels and endls.
Thanks! That fixed it.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/195071/
I updated the original post, with the updated code.

Any suggestions on lining up the decimals?

Thanks!
closed account (48T7M4Gy)
Well done. Thanks for making it one thread. I'll have a look and get back if somebody doesn't already have a reply.
closed account (48T7M4Gy)
Yeah, it looks like you need to read up on setw() etc because putting in blank spaces "works" but becomes impossible.

http://www.cplusplus.com/reference/iomanip/setw/
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw

int main () {
    
    std::string month = "August";
  
    double balance = 45.678988;
    double interestPaidMonth = 567.8901;
  
    //Making sure outputs have two decimal places and it lined up
    std::cout << std::setprecision(3) << std::fixed;
    
    //Outputing the row
    std::cout << month << std::setw(10) << balance << std::setw(12)<< interestPaidMonth << std::endl;
    std::cout << month << std::setw(10) << balance + 1.2345 << std::setw(12)<< interestPaidMonth + 897.231098 << std::endl;

    return 0;
}

August	    45.679     567.890
August	    46.913    1465.121
 
Exit code: 0 (normal program termination)
Last edited on
Thanks kemort.

I took your suggestion and learned about it, and how to effectively use the setw(10) between each of my variables output.

it worked perfectly and the decimals lined up.

Now I am requesting help on how to output all of this program output into a TXT.file?

Does C++ have a way to do that?

Thanks! :)
closed account (E0p9LyTq)
If you can output to the console using std::cout, you can output to a file using a filestream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <fstream>

int main()
{
   std::string li = "lorem ipsum";

   std::cout << li << "\n";

   std::ofstream ofs ("test.txt", std::ofstream::out);

   ofs << li << "\n";

   ofs.close();
}


lorem ipsum


contents of "test.txt":
lorem ipsum
I added:

ofstream ofs ("lab6.txt");
ofs << all the variables I want in << endl;
ofs.close();

The program had errors.

I edited the code to reflect my changes.

Is it because of my placement, or the syntax?

The errors came out:
In function 'int main()':
87:5: error: 'in' was not declared in this scope
88:5: error: 'ofs' was not declared in this scope
In function 'void printResults(int&, double&, double&, double&, double&)':
115:5: error: 'ofs' was not declared in this scope
In function 'double calculate(double&, double&, double&, double&, double&, double&, int&)':
176:1: warning: no return statement in function returning non-void [-Wreturn-type]

Last edited on
closed account (E0p9LyTq)
Without seeing your source code after you made your changes it is impossible to say what the problems are.

Remember we are not in the room, looking at your computer. :)
Ah sorry.

The source code is updated in full in the original first post.

Is that what you meant?
closed account (E0p9LyTq)
Line 87, you are closing a non-declared object in.close();

Line 88, you opened a file object at line 66, calling it out. Why are you using ofs.close();? I think you meant out.close();.

Line 115, where did you declare your ofs object? Not in the function, that's for sure.

You declared your calculate function to return a double, but you don't return anything. You pass references into the function so any change made to the variables get returned when the function exits. Use the void return type.
Last edited on
Ah, thanks!
I took your suggestions, and edit it, and now the source code is correct.

When I run it, it creates a txt file.

But when I run the program, the program outputs the correct decimal places.

But I am wondering why the output txt file do not have the correct decimal places.

Can you not format output in the txt file?
closed account (48T7M4Gy)
Yes you can format it the way you want. Instead of directing the stream to cout you direct it to the text file as FG said.

http://www.cplusplus.com/doc/tutorial/files/

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
#include <iostream>     // std::cout, std::endl
#include <fstream>
#include <iomanip>      // std::setw

int main ()
{
    
    std::string month = "August";
    
    double balance = 45.678988;
    double interestPaidMonth = 567.8901;
    
    std::ofstream myfile ("streamer.txt");
    if (myfile.is_open())
    {
        myfile << "Month       Bal     Interest\n";
        myfile << "----------------------------\n";
        myfile << std::fixed;
        
        myfile
        << month
        << std::setw(10) << std::setprecision(2) << balance
        << std::setw(12) << std::setprecision(3) << interestPaidMonth
        << '\n';
        
        myfile
        << month
        << std::setw(10) << std::setprecision(2) << balance + 1.2345
        << std::setw(12) << std::setprecision(3) << interestPaidMonth + 897.231098
        << '\n';
        
        myfile.close();
    }
    else std::cout << "Unable to open file\n";
    
    return 0;
}


stringstreams are the follow on step to this to avoid duplication if you want simultaneous cout output.


Month       Bal     Interest
----------------------------
August     45.68     567.890
August     46.91    1465.121

Last edited on
Thanks!!

I finished the program, and from doing this, I gained a lot of knowledge and newfound capabilities of what C++ can do.

Appreciate all the directions and directing me to good resources to learn.
Nevermind, I am back.

The entire code works. It grab the data from the source file, then output it into another file.

But,

I noticed the following feedbacks:


What does the below mean?


The functions: printHeader(), printResults() and calculate() must be called from main().

Since output functions do not change values of what is being printed out, none of the parameters for printHeader() or printResults() should be reference variables with the exception of the reference to the file.
closed account (E0p9LyTq)
What is generating this printHeader(), printResults() and calculate() must be called from main(). message? Your compiler? When you run the program? UM-possible to say what the problem is without telling us where the error/warning is from. :)
Pages: 12