Console seems to print everything in file up to break

Hello,

I am trying to get specific things from a file but my program keeps getting everything from that file up to the break point.


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
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

int main(){

    cout << "Remember to please use ALL CAPITALS for this program" << endl;
    cout << "Try not to crash it... happy pricing!" << endl;
    cout << endl << endl;

//=============================================================================================
    //variables being used
    string dfSize = string();
    int ftLong = int();
    double clearPrice = double();
    double bronzePrice = double();
    double blackPrice = double();
    int pricingWidth = int();
    int pricingHeight = int();
    int wallSize = int();
    string trim = string();
    string df = string();
    string ano = string();
    string throatSize = string();
    bool found = false;
    double multiplier = double();


//=============================================================================================
    //ask info needed

    cout << "What is your multiplier?" << endl;
    cin >> multiplier;


    cout << "What door frame do you need? (3X7, 3X8, 4X9, ETC..)" << endl;
    cin >> df;
    cout << "What is your wall size? (318, 319, 320)" << endl;
    cin >> wallSize;
    cout << "What is your ano? (CC, CB, BK)" << endl;
    cin >> ano;
    cout << "What trim do you want? (302, 304, 304-2)" << endl;
    cin >> trim;
    cout << endl << endl;

    if (wallSize == 318 && trim == "302"){
        ifstream read("318 - 302 TRIM.txt");
        throatSize = "4-7/8";
            if (df == "3X7" && ano == "CC"){
                while (found != true) {

                read >> dfSize >> ftLong >> clearPrice >> bronzePrice >> blackPrice;
                    if (dfSize == "3X7"){
                    found = true;
                    }
                clearPrice = clearPrice * multiplier;
                cout << dfSize << " " << throatSize << " " << "clear ano " << trim << " trim " << setprecision(2) << fixed << clearPrice << endl;
                }
            }

            else if (df == "3X8" && ano == "CC"){
                while (found != true) {
                read >> dfSize >> ftLong >> clearPrice >> bronzePrice >> blackPrice;
                    if (dfSize == "3X8"){
                    found = true;
                    }
                clearPrice = clearPrice * multiplier;
                cout << dfSize << " " << throatSize << " " << "clear ano " << trim << " trim " << setprecision(2) << fixed << clearPrice << endl;
                }
            }

            else if (df == "4X7" && ano == "CC"){
                while (found != true) {
                read >> dfSize >> ftLong >> clearPrice >> bronzePrice >> blackPrice;
                    if (dfSize == "4X7"){
                    found = true;
                    }
                clearPrice = clearPrice * multiplier;
                cout << dfSize << " " << throatSize << " " << "clear ano " << trim << " trim " << setprecision(2) << fixed << clearPrice << endl;
                }
            }
    }

    return 0;

}



The file has this:
3X7 17 163.26 200.71 225.80
3X8 19 173.09 214.51 241.33
3X9 21 191.31 237.09 266.74
3X10 23 204.81 254.54 286.38
4X7 18 172.86 212.52 239.08
4X8 20 182.20 225.80 254.03
4X9 22 200.42 248.38 279.44



Whenever I input let say "4X7", it prints all the sizes and prices.
Hello jvxn21,

It took me a while to figure out the very first if statement needs "wallSize" of 318 and "trim" of "302" for the rest to work.

I only worked with the "4X7" because it was several lines into the input file. What I found is that lines 82 and 83 need to come after line 80. Otherwise it prints every line read from the file.

Some hints/suggestions:

Calling the input stream "read" may seem reasonable at first, but it gave me the wrong impression, as in you were trying to read a binary file. A name that is more descriptive would be "inFile" or the opposite "outFile". It makes the code easier to understand.

Your use of setprecision is redundant. This only needs to be done once and I put it about line 49 as: std::cout << std::fixed << std::showpoint << std::setprecision(2);. The "showpoint" allows ".00" to be displayed otherwise it is left off.

A suggestion for testing the program:


int wallSize = int(318);
string trim = string("302");
string df = string("4X7");
string ano = string("CC");
double multiplier = double(2);


And give them a value. Then comment out the lines 36 - 48 and you can test the program without having to enter each field when the program runs.

I like to set up the input as:
1
2
cout << "What is your multiplier? ";
cin >> multiplier;

Notice that the "cout" statement has a space before the closing quote and the "endl" is missing. this allows the "cin" to be on the same line. To me it is a cleaner way of doing input rather than having the cursor just hanging there on the next line.

Instead of relaying on the user to enter a capital when needed you might want to consider a function, using "std::toupper()", header file "cctype", to make sure every letter input is in upper case.

Hope that helps,

Andy
What should these instruction do?
1
2
3
4
5
6
7
. . .
double blackPrice = double();
int pricingWidth = int();
int pricingHeight = int();
int wallSize = int();
string trim = string();
. . .


Do you want your variables to be initialised to 0 as soon as they are created?
This is a more conventional way:
1
2
3
double blackPrice {};
int wallSize {};
string trim;    // std::strings usually don’t need to be initialised 


Whenever I input let say "4X7", it prints all the sizes and prices.

Let’s have a look at your code (lines 76-85):
1
2
3
4
5
6
7
8
9
10
else if (df == "4X7" && ano == "CC"){
    while (found != true) {
        read >> dfSize >> ftLong >> clearPrice >> bronzePrice >> blackPrice;
        if (dfSize == "4X7"){
            found = true;
        }
        clearPrice = clearPrice * multiplier;
        cout << dfSize << " " << throatSize << " " << "clear ano " << trim << " trim " << setprecision(2) << fixed << clearPrice << endl;
    }
}

If df == "4X7", than, since found == false, it enters the while-loop and iterates through it until dfSize == "4X7".
At every iteration, it outputs the contents of the variables dfSize, throatSize, trim and clearPrice.

Please note: declaring all your variables at the beginning of your functions may introduce hard to find bugs.
What follows is your code rewritten in a more ‘traditional’ way: you could use it to spot some of them – for example, variable “throatSize” (in this code “throat_size”).
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
#include <cmath>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::cout << "Remember to please use ALL CAPITALS for this program\n"
                 "Try not to crash it... Happy pricing!\n\n\n";

    //==========================================================================
    //ask info needed

    std::cout << "What is your multiplier? ";
    double multiplier {};
    std::cin >> multiplier;

    std::cout << "What door frame do you need (3X7, 3X8, 4X9, ETC..)? ";
    std::string df;
    std::cin >> df;

    std::cout << "What is your wall size (318, 319, 320)? ";
    int wall_size {};
    std::cin >> wall_size;

    std::cout << "What is your ano (CC, CB, BK)? ";
    std::string ano;
    std::cin >> ano;

    std::cout << "What trim do you want (302, 304, 304-2)? ";
    std::string trim;
    std::cin >> trim;


    if (wall_size == 318 && trim == "302") {
        std::ifstream read("318-302_TRIM.txt");
        if(!read) {
            std::cout << "Can't open file 318-302_TRIM.txt.\n";
            return 0;
        }
        if (df == "3X7" && ano == "CC") {
            bool found = false;
            while (!found) {
                std::string df_size;
                int ft_long {};
                double clear_price {};
                double bronze_price {};
                double black_price {};
                read >> df_size >> ft_long >> clear_price >> bronze_price >> black_price;
                if (df_size == "3X7") {
                    found = true;
                }
                clear_price = clear_price * multiplier;
                std::string throat_size = "4-7/8";
                std::cout << "\n\n" << df_size << ' '
                          << throat_size << "  clear ano "
                          << trim << " trim "
                          << std::setprecision(2) << std::fixed << clear_price << '\n';
            }
        }

        else if (df == "3X8" && ano == "CC") {
            bool found = false;
            while (found != true) {
                std::string df_size;
                int ft_long {};
                double clear_price {};
                double bronze_price {};
                double black_price {};
                read >> df_size >> ft_long >> clear_price >> bronze_price >> black_price;
                    if (df_size == "3X8") {
                        found = true;
                    }
                clear_price = clear_price * multiplier;
                std::string throat_size = "4-7/8";
                std::cout << "\n\n" << df_size << " " << throat_size << " clear ano "
                          << trim << " trim "
                          << std::setprecision(2) << std::fixed << clear_price << '\n';
            }
        }

        else if (df == "4X7" && ano == "CC"){
            bool found = false;
            while (found != true) {
                std::string df_size;
                int ft_long {};
                double clear_price {};
                double bronze_price {};
                double black_price {};
                read >> df_size >> ft_long >> clear_price >> bronze_price >> black_price;
                if (df_size == "4X7") {
                    found = true;
                }
                clear_price = clear_price * multiplier;
                std::string throat_size = "4-7/8";
                std::cout << "\n\n" << df_size << " " << throat_size << " clear ano "
                          << trim << " trim "
                          << std::setprecision(2) << std::fixed << clear_price << '\n';
            }
        }
    }
    return 0;
}


And, as Handy Andy said
Handy Andy wrote:
Calling the input stream "read" … gave me the wrong impression

Very bad, indeed! There’s a function “read()” in the standard library, which you can access after having included <iostream>, as you did. You also wrote using namespace std; which introduces all the standard library names in the global namespace: IMHO, that means you’re doing your best to shoot yourself in the foot.
@Enoizat,

You also wrote using namespace std; which introduces all the standard library names in the global namespace: IMHO, that means you’re doing your best to shoot yourself in the foot.

I like that. especially the last part.

Andy
@Enoizat, @ Handy Andy

Hahah, I'm sorry I just started learning on my own and practicing by taking on random exercises

On another note, I've come to an understanding where if I use:
 
using namespace std;


at the beginning I don't have to include the "std::" before cout or cin or any standard functions. Am I wrong for thinking like this? In the real world for jobs, is it a good habit to use the "std::" before every function or to just define it at the beginning?
Also, I did Objective-C before I started learning C++.
The way I learned to name my variables was:
1
2
int mainNumber;
string firstName;


But, @Enoizat does:
1
2
int main_number;
string first_name;


Is there a difference? Is one more easier to understand or preferred more than the other way?
They're just names. The only thing that "matters" is whether the names help you understand clearly and easily what they represent. Part of that includes whether the style makes your code more readable or not.

Obviously, that last part is subjective. What you find more readable may not be what another coder finds more readable.
Last edited on
So I tried what @Enoizat said... now I have two questions.

First, I had to change all variables like from this:
1
2
int ft_long {};
double clear_price {};

into this:
1
2
int ft_long = int();
double clear_price = double();


I kept receiving an error message about it
extended initializer lists only available with -std=c++11 or -std=gnu++11|


Why is that?


Second question, I still get all the output of the loop:

Remember to please use ALL CAPITALS for this program
Try not to crash it... Happy pricing!


What is your multiplier? .75
What door frame do you need (3X7, 3X8, 4X9, ETC..)? 3X8
What is your wall size (318, 319, 320)? 318
What is your ano (CC, CB, BK)? CC
What trim do you want (302, 304, 304-2)? 302


3X7 4-7/8 clear ano 302 trim 122.44


3X8 4-7/8 clear ano 302 trim 129.82

Process returned 0 (0x0)   execution time : 16.572 s
Press any key to continue.



How can I get it so I only get the information of the size I need?
Hello jvxn21,

You have the output, "cout" statements, in the wrong place. Refer to message http://www.cplusplus.com/forum/beginner/244010/#msg1081066 You must have missed it.

Andy
jvxn21 wrote:
1
2
int ft_long = int();
double clear_price = double();

I think you’d better buy a book or attend a course: it will take you a long time to learn C++ if you want to adjust its syntax to your tastes.

In short:
a) you can declare a variable just by writing:
int myvar; // myvar now contains a random value
You can initialise your variables as soon as you declare them by one of these methods:
1
2
int myvar = 666; // old way, still valid, but admits narrowing
int myvar { 666 }; // 'modern' style, safer because doesn't admit narrowing 


You can instruct your compiler to recognise which set of rules you are playing with by adding a proper flag, for example -std=c++14. Since I can’t know which compiler you are working with in which environment, I can’t help you about this.

b)
I still get all the output of the loop
I stated:
Enoizat wrote:
What follows is your code rewritten in a more ‘traditional’ way
You’re getting the same error because I didn’t modified your code, so the problem is still there.
This block of code:
1
2
3
4
5
6
7
8
9
10
            else if (df == "4X7" && ano == "CC"){
                while (found != true) {
                read >> dfSize >> ftLong >> clearPrice >> bronzePrice >> blackPrice;
                    if (dfSize == "4X7"){
                    found = true;
                    }
                clearPrice = clearPrice * multiplier;
                cout << dfSize << " " << throatSize << " " << "clear ano " << trim << " trim " << setprecision(2) << fixed << clearPrice << endl;
                }
            }

is badly indented, therefore it seems you can’t spot the issue.
The above block of code iterates through the files, loads what written into some variables and, at every iteration, outputs the content of some variables.

c)
is it a good habit to use the "std::" before every function
(Please note: not only functions: all the names in the standard C+ library are included into the ‘std’ namespace.)
I think you’d better don’t use it unless you’re an expert. This is a short answer (I usually don’t like to address people to long, detailed pages, but you can easily find other explanations online):
https://isocpp.org/wiki/faq/coding-standards#using-namespace-std

Happy coding!
Topic archived. No new replies allowed.