I just started "Introduction to C " this semester, and I'm stuck on an exercise that involves taking data from a table and writing a program that will let you input and display each item (line by line) showing the Item, Cost, and calculated Total Cost.
The program I've written works perfectly for the first item, but I need it to repeat the same 3 questions (allowing user input of different items/costs/discounts each time it repeats the questions), while printing the calculated total after each question is answered. I'm assuming this will involve either a do-while
loop or a for
loop, but I can't figure out how to integrate what I've already written into a loop.
#include <iostream>
using namespace std;
int main()
{
string Item;
float Cost;
float Discount;
float TotalCost;
cout << "What is the item? \n";
cin >> Item;
cout << "What is the cost? \n";
cin >> Cost;
cout << "What is the discount? \n";
cin >> Discount;
TotalCost = Cost - Discount;
cout << Item << "'s Total Cost is " << TotalCost;
return 0;
}
I've tried making a for
loop (code I've tried below), but it doesn't work, and I haven't been able to find any loop examples in my book or online that involve accepting user input each time the process loops.
#include <iostream>
using namespace std;
int main()
{
string Item;
float Cost;
float Discount;
float TotalCost;
for (int a = 0; a < 5; a )
{
cout << "What is the item? \n";
cin >> Item;
cout << "What is the cost? \n";
cin >> Cost;
cout << "What is the discount? \n";
cin >> Discount;
TotalCost = Cost - Discount;
cout << Item << "'s Total Cost is " << TotalCost;
}
return 0;
}
CodePudding user response:
Two things:
- We only run it 4 times, so it should be
a < 4
- When you get the item, use the
std::getline
function. This will return everything the user enters, including spaces. - We need to ignore empty lines. When we start the loop again, there's an empty line because the user pressed "enter" after entering the discount. So we need to put the
getline
in a loop, and read lines until finding one that's not empty. - When you print the total cost at the end, put a newline so when the loop body starts again, we're on a new line.
Updated code:
#include <iostream>
using namespace std;
int main()
{
string Item;
float Cost;
float Discount;
float TotalCost;
for (int a = 0; a < 4; a ) // (1)
{
cout << "What is the item? \n";
do {
// (2) Use the std::getline function to get the entire line.
std::getline(std::cin, Item);
// (3) Empty lines should be ignored. If the line is empty,
// Get a line again.
} while(Item.size() == 0);
cout << "What is the cost? \n";
cin >> Cost;
cout << "What is the discount? \n";
cin >> Discount;
TotalCost = Cost - Discount;
// (4) Put a newline here at the end to make sure the buffer
// Gets flushed before the loop starts again.
cout << Item << "'s Total Cost is " << TotalCost << '\n';
}
return 0;
}
You don't need to use an array for Cost, Discount, or TotalCost, because it's fine to overwrite those with each new loop iteration.
CodePudding user response:
Nice first try.
You need to use arrays, primitives like int, float and double only represent ONE number.
#include <iostream>
#include <array>
#include <string>
int main()
{
std::array<std::string,4> Item;
std::array<float,4> Cost;
std::array<float,4> Discount;
std::array<float,4> TotalCost;
for (size_t i = 0; i < 4; i)
{
std::cout << "What is the item? \n";
std::getline(std::cin, Item[i]);
std::cout << "What is the cost? \n";
std::cin >> Cost[i];
std::cout << "What is the discount? \n";
std::cin >> Discount[i];
TotalCost[i] = Cost[i] - Discount[i];
std::cout << Item[i] << "'s Total Cost is " << TotalCost[i];
}//End for
return EXIT_SUCCESS;
}
This is fine in your case as you're just starting to learn, but please note: This is rarely the way we do it in real life.
You'd use a while
loop and ask the user if they want to enter more items after each entry, this allows your program to be flexible in how much data it processes. (Note this requires the use of an std::vector<T>
as that structure is automatically resizable)
We'd also use objects, but you're likely not going to be working with those for a bit yet. Best to get the basics down.
Some other tips:
- avoid
using namespace std;
, this can cause problems when you start to work with libraries std::string
is an object, not a primitive, so you need#include <string>
- In C , array indexes start at zero, so your loop should be
i < 4
NOTi < 5
- Favor
i
instead ofi
, some (albeit very old) compilers may generate more efficient code - In
for
loops, usesize_t
instead ofint
.size_t
is always unsigned (no negative numbers) and will be a 32-bitint
on 32-bit systems and (usually) a 64-bitint
on 64-bit systems. This is more efficient, and prevents certain indexing errors - When exiting successfully, return
EXIT_SUCCESS
, otherwiseEXIT_FAILURE
(albeit this one is kind of a personal preference, and they are both macros, which are usually considered evil in C )