0

The following loop, is supposed to terminate when T is entered as the type my following code. When I enter T to check if loop determine if it terminates I just get a blank line how could I solve this problem?

#include <iostream>
#include <iomanip>
using namespace std;
void get_user_input(char&, int&, int&, int&, int&);
float compute_item_cost(char, int, int, int, int);
const float pine_cost = 0.89;
const float fir_cost = 1.09;
const float cedar_cost = 2.26;
const float maple_cost = 4.50;
const float oak_cost = 3.10;
int main()
{
    int quanity, height, width, length;
    string name_of_wood;
    char type;//declare variables
    get_user_input(type, quanity, height, width, length);

    do
    {

        float cost = compute_item_cost(type, quanity, height, width, length);
        if (type == 'P') {
            cout << "Pine" << cost;
            cout << "\n";
            get_user_input(type, quanity, height, width, length);
        }
    }
    while (type != 'T');
    cout << "bad input";
}
void get_user_input(char& type, int& quanity, int& height, int& width, int& length)
{
    cout << "Enter the wood type";
    cin >> type >> quanity >> height >> width >> length;
}
float compute_item_cost(char type, int quanity, int height, int width, int length)
{
    float compute_cost;
    float price;
    if (type == 'P') {
        compute_cost = (height*width*length) / 12.0;
        return compute_cost*quanity*pine_cost;
    }
    //compute_cost = (height*width*length) / 12.0;
    //return compute_cost*quanity*

    compute_cost = (height*width*length) / 12.0;
    return compute_cost*4.50*quanity;
5
  • What do you expect to happen? If I enter T the program ends. Commented Mar 26, 2017 at 1:23
  • you have no return statement on your INT main() Commented Mar 26, 2017 at 1:25
  • @J3STER You don't need one. In C++ a lack of return from main default's to return 0; It's in the standard. Commented Mar 26, 2017 at 1:26
  • @J3STER the return is not required (even if it's better with it), it will compile anyway Commented Mar 26, 2017 at 1:26
  • basically if the type == "T" im susposed to output the total of the cost but i havent got that part yet im just checking if i output when the loop does terminate and id dont Commented Mar 26, 2017 at 1:28

2 Answers 2

1

I'm thinking if you enter T you shouldn't have to enter the rest of the stuff to continue (and terminate the program). So maybe change get_user_input like this:

void get_user_input(char& type, int& quanity, int& height, int& width, int& length)
{
    cout << "Enter the wood type";
    cin >> type;
    if (type != 'T')
    {
        cin >> quanity >> height >> width >> length;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Don't forget to use std::endl to flush and insert a newline on std::cout, or even use std::flush.

std::cout << "Some text" << std::endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.