I have this CODE to take user input and validate it to increase the user BTC in User wallet.
I need some Explanation of this CODE how it works.
The Code:
#include <iostream>
using namespace std;
int main()
{
int btc_input, result_btc, btc_validation, btc_validation_1_input, btc_validation_2_input;
result_btc = 0;
btc_validation = 0;
btc_validation_1_input = 0;
btc_validation_2_input = 0;
for (; btc_validation < 3; btc_validation++)
{
cout << "Enter the Value " << (btc_validation + 1) << " + " ;
cin >> btc_input;
cout << "Enter the Value " << (btc_validation + 1) << " + " ;
cin >> btc_input;
btc_validation_1_input == btc_validation + btc_input;
cout << "Enter the Value " << (btc_validation_1_input + 1) << " + " ;
cin >> btc_validation_2_input;
result_btc = btc_validation_1_input + btc_validation_2_input;
result_btc *= btc_validation_2_input;
break;
}
cout << result_btc << '\n';
cout << "Your BTC Wallet is Increased by " << result_btc << " coins " << '\n';
}
Your Code is not an API in any case but a code, and be aware with scammers. The code you have is working the same way as I will explain by editing and converting your code in a minimum form for your better understanding.
#include <iostream>
using namespace std;
int main()
{
int btc_input, result_btc, btc_validation;
result_btc = 0;
btc_validation = 0;
for (; btc_validation < 3; btc_validation++)
{
cout << "Enter the number >> [" << btc_validation + 1 << "] " ;
cin >> btc_input;
result_btc += btc_input;
}
cout << (result_btc *= btc_input) << '\n';
cout << "You entered " << result_btc << " numbers " << '\n';
}
Related
I'm making a code where users can type in numbers that will be stored in an array which is inside a struct. However, sometimes, the value that I input is not the same as the one that is stored/display. This problem is not very consistent. For example, when I input 10, it could either be shown as: 6384644, 6382852, or actually 10. I am very confused for this, I've tried changing the array data type using int, long, and double, but to no avail.
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int n,x,totalAge = 0,maxAge = 0,avgAge = 0,maxGoals = 0,bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> n;
cout << "Input the number of games: ";
cin >> x;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
long goals[];
};
players goals[x];
players playerList[n];
for (int i = 0; i < n; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin.ignore();
getline (cin, playerList[i].name);
cout << "Input player's current team: ";
getline (cin, playerList[i].currTeam);
cout << "Input player's shirt number: ";
cin >> playerList[i].shirtn;
cout << "Input player's age: ";
cin >> playerList[i].age;
cout << "Input player's height (m): ";
cin >> playerList[i].height;
cout << "Input player's weight (kg): ";
cin >> playerList[i].weight;
cout << endl;
for (int a = 0; a < x; a++) {
playerList[i].goals[a] = 0;
playerList[i].totalGoals = 0;
}
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> playerList[i].goals[a];
playerList[i].totalGoals += playerList[i].goals[a];
}
if (playerList[i].totalGoals > maxGoals) {
maxGoals = playerList[i].totalGoals;
bestPlayer = i;
}
if (playerList[i].age > maxAge) {
maxAge = playerList[i].age;
oldest = i;
}
totalAge += playerList[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << playerList[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << playerList[i].currTeam << endl;
cout << "Shirt Number: " << playerList[i].shirtn << endl;
cout << "Age: " << playerList[i].age << endl;
cout << "Height: " << playerList[i].height << " m" << endl;
cout << "Weight: " << playerList[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << playerList[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / n;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << playerList[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << playerList[bestPlayer].name << ", shirt number: " << playerList[bestPlayer].shirtn << ". With total goals of: " << playerList[bestPlayer].totalGoals << endl;
}
Your code's corrected version would be this:
#include<string>
#include<iostream>
#include<vector> //for vector
using namespace std;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
vector<long> goals; //used vector instead of array
};
int main () {
int N, X;
int totalAge = 0, maxAge = 0, avgAge = 0, maxGoals = 0, bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> N;
cout << "Input the number of games: ";
cin >> X;
players player[X];
for (int i = 0; i < N; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin>>player[i].name;
cout << "Input player's current team: ";
cin>>player[i].currTeam;
cout << "Input player's shirt number: ";
cin >> player[i].shirtn;
cout << "Input player's age: ";
cin >> player[i].age;
cout << "Input player's height (m): ";
cin >> player[i].height;
cout << "Input player's weight (kg): ";
cin >> player[i].weight;
cout << endl;
player[i].totalGoals = 0;
for (int a = 0; a < X; a++) {
long G;
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> G;
player[i].goals.push_back(G);
player[i].totalGoals += G;
}
if (player[i].totalGoals > maxGoals) {
maxGoals = player[i].totalGoals;
bestPlayer = i;
}
if (player[i].age > maxAge) {
maxAge = player[i].age;
oldest = i;
}
totalAge += player[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < N; i++) {
cout << player[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << player[i].currTeam << endl;
cout << "Shirt Number: " << player[i].shirtn << endl;
cout << "Age: " << player[i].age << endl;
cout << "Height: " << player[i].height << " m" << endl;
cout << "Weight: " << player[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < X; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << player[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / N;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << player[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << player[bestPlayer].name << ", shirt number: " << player[bestPlayer].shirtn << ". With total goals of: " << player[bestPlayer].totalGoals << endl;
return 0;
}
I am trying to make a simple program in c++ that takes the price and number of items purchases, stores it in arrays, and then outputs the totals for each item in a tabular format. However, when I multiply the numbers in my code, I get totally strange answers! Can someone please enlighten me as to what's going on?
Code:
#include <iostream>
using namespace std;
int main(int argc, _TCHAR* argv[]) {
float price[4], tot[4];
int amt[4];
cout << "Please enter the price and amount of 4 items:\n";
for (int i = 0; i<4; i++) {
cout << "Price of item " << i + 1 << ": ";
cin >> price[i];
cout << "Amount of item " << i + 1 << ": ";
cin >> amt[i];
if (price[i] <= 0 || amt[i] <= 0) {
cout << "Invalid Input Entry!\n";
break;
}
tot[i] = price[i] * amt[i]; // I can't really see how I could have messed this up...
}
cout << "Total\t\tPrice\t\tAmount\n";
cout << "-----\t\t-----\t\t------\n";
for (int i = 0; i < 4; i++) {
cout << "$" << fixed << cout.precision(2) << tot[i] << "\t\t$" << price[i] << "\t\t" << amt[i] << endl;
}
system("pause");
return 0;
}
Output:
The problem is that you are outputting the return value of cout.precision(2) (which returns the previous precision, in this case 6 initially and then 2 afterwards) in front of each total price.
You need to either:
not pass the return value of cout.precision() to operator<<:
cout << "$" << fixed;
cout.precision(2);
cout << tot[i] << ...
or, call precision() one time before entering the loop:
cout.precision(2);
for (int i = 0; i < 4; i++) {
cout << "$" << fixed << tot[i] << "\t\t$" << price[i] << "\t\t" << amt[i] << endl;
}
use the std::setprecision() stream manipulator instead of calling cout.precision() directly:
#include <iomanip>
for (int i = 0; i < 4; i++) {
cout << "$" << fixed << setprecision(2) << tot[i] << "\t\t$" << price[i] << "\t\t" << amt[i] << endl;
}
or
#include <iomanip>
cout << setprecision(2);
for (int i = 0; i < 4; i++) {
cout << "$" << fixed << tot[i] << "\t\t$" << price[i] << "\t\t" << amt[i] << endl;
}
On a side note, you should not use \t characters to control the formatting of your table. Use stream manipulators like std::setw(), std::left, etc instead:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int maxItems = 4;
string moneyStr(float amount)
{
ostringstream oss;
// in C++11 and later, you can replace this with std::put_money() instead:
// http://en.cppreference.com/w/cpp/io/manip/put_money
//
// oss << showbase << put_money(amount);
oss << "$" << fixed << setprecision(2) << amount;
return oss.str();
}
int main(int argc, _TCHAR* argv[])
{
float price[maxItems], tot[maxItems];
int amt[maxItems];
int cnt = 0;
cout << "Please enter the price and amount of " << maxItems << " items:" << endl;
for (int i = 0; i < maxItems; ++i)
{
cout << "Price of item " << i + 1 << ": ";
cin >> price[i];
cout << "Amount of item " << i + 1 << ": ";
cin >> amt[i];
if (price[i] <= 0 || amt[i] <= 0) {
cout << "Invalid Input Entry!" << endl;
break;
}
tot[i] = price[i] * amt[i];
++cnt;
}
cout << left << setfill(' ');
cout << setw(16) << "Total" << setw(16) << "Price" << setw(16) << "Amount" << endl;
cout << setw(16) << "-----" << setw(16) << "-----" << setw(16) << "------" << endl;
for (int i = 0; i < cnt; i++) {
cout << setw(16) << moneyStr(tot[i]) << setw(16) << moneyStr(price[i]) << setw(16) << amt[i] << endl;
}
system("pause");
return 0;
}
Live Demo
I have a file that includes this information:
Bev Powers
3
76 81 73
Chris Buroughs
5
88 90 79 81 84
Brent Mylus
2
79 81
I have a count controlled loop that will do the first 3 lines and use the information correctly but I am struggling with a loop that will reuse the the loop until all the information is displayed from the file regardless of how many golfers with matches are on the file. I am asking for pointers in the right direction, any assistance would be appreciated.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream inScores;
string filename;
string name;
int loopCount, matchScore;
int count = 1;
float mean = 0;
float adder = 0;
int main()
{
cout << endl << "Enter the golfer's filename: ";
getline(cin,filename);
cout << endl;
inScores.open(filename.c_str());
if(!inScores)
{
cout << "** " << filename << " does not exist. Please ";
cout << "check the spelling and rerun ";
cout << "the program with an existing golfer file. ** " << endl << endl;
return 1;
}
getline(inScores,name);
inScores >> loopCount;
cout << name << " has " << loopCount << " matches with scores of" << endl << endl;
inScores >> matchScore;
while (count <= loopCount)
{
cout << "Match " << count << ": " << matchScore << endl;
adder = adder + matchScore;
adder = adder + matchScore;
inScores >> matchScore;
count++;
}
cout << endl;
int(mean) = .5 + (adder / loopCount);
cout << "The mean score is " << mean << endl << endl;
inScores.close();
return 0;
}
As stated using loops will be necessary to get what you want. Also since the getline and extraction return false if they fail you can use them for the test in the loop:
ifstream inScores;
string filename;
string name;
int loopCount , matchScore;
int count = 1;
float mean = 0;
float adder = 0;
int main()
{
cout << endl << "Enter the golfer's filename: ";
getline( cin , filename );
cout << '\n';
inScores.open( filename );
if ( !inScores )
{
cout << "** " << filename << " does not exist. Please ";
cout << "check the spelling and rerun ";
cout << "the program with an existing golfer file. ** " << "\n\n";
return 1;
}
while ( getline( inScores , name ) )
{
if ( inScores >> loopCount )
{
cout << name << " has " << loopCount << " matches with scores of" << "\n\n";
}
else
{
cout << "File read error";
return 1;
}
for ( int count = 1; count <= loopCount; count++ )
{
if ( inScores >> matchScore )
{
cout << "Match " << count << ": " << matchScore << '\n';
adder = adder + matchScore;
}
else
{
cout << "File read error";
return 1;
}
}
}
cout << '\n';
int( mean ) = .5 + ( adder / loopCount );
cout << "The mean score is " << mean << "\n\n";
inScores.close();
return 0;
}
I have a text file of cars:
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 190.83 1
2010 Toyota Corolla 50.36 1
I am trying to find the max which is the float but I am having trouble finding it as well as finding the rental cost of the cars. I have this so far but am still having trouble.
#include <iostream>
#include <fstream>
using namespace std;
struct car
{
int year;
char make[10];
char model[10];
float price;
int available;
} ;
void menu();
// Main Function
int main ()
{
// declare variables
int carAmount = 10;
int choice;
car carLib[carAmount];
char filename[10];
ifstream carInData;
float mostExpensive = 0;
int MostExpensiveIndex;
int count = 0;
int days;
int rentalCost = 0;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
menu();
carInData.open(filename);
cin >> choice;
if(carInData.is_open());
{
// read list of names into array
for(cout; count < carAmount; count++){
carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
switch (choice){
case 1:
if(carLib[count].available == 1)
cout << " Available ";
else
cout << " Unavailable ";
cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << " " << "\n";
break;
case 2:
cout << " Enter car number and how many days " << "\n";
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
cin >> carLib[count].price;
rentalCost += days * count;
cout << " Rental Cost for " << days << " days is " << rentalCost;
break;
case 3:
MostExpensiveIndex = count;
for(size_t carIndex = 0; carIndex < count; ++carIndex){
if(carLib[carAmount].price <= mostExpensive) continue;
mostExpensive = carLib[carIndex].price;
MostExpensiveIndex = carIndex;
}
const car & carI = carLib[MostExpensiveIndex];
cout << " Most Expensive car is: " << MostExpensiveIndex << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n";
break;
}
}
}
return 0;
}
void menu(){
cout << " 1 - Show Cars\n";
cout << " 2 - Rental Cost\n";
cout << " 3 - Most Expensive Car\n";
}
My output is displaying all my cars instead of the maximum one. This is the screenshot of the output
Replace:
if(carLib[carAmount].price <= mostExpensive) continue;
with
if(carLib[carIndex].price <= mostExpensive) continue;
Correct code is:
#include <iostream>
#include <fstream>
using namespace std;
struct car {
int year;
char make[10];
char model[10];
float price;
int available;
} ;
void menu();
// Main Function
int main ()
{
// declare variables
int carAmount = 10;
int choice;
car carLib[carAmount];
char filename[10];
ifstream carInData;
float mostExpensive = 0;
int MostExpensiveIndex;
int count = 0;
int days;
float rentalCost = 0;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
menu();
carInData.open(filename);
cin >> choice;
if (carInData.is_open()) {
// read list of names into array
for (; count < carAmount; count++) {
carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
}
}
switch (choice) {
case 1:
for (int carIndex = 0; carIndex < carAmount; ++carIndex){
if (carLib[carIndex].available == 1)
cout << " Available ";
else
cout << " Unavailable ";
cout << carLib[carIndex].year << " " << carLib[carIndex].make << " " << carLib[carIndex].model << " " << carLib[carIndex].price << " " << "\n";
}
break;
case 2:
cout << " Enter car number and how many days " << "\n";
cin >> count;
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
rentalCost = days * carLib[count].price;
cout << " Rental Cost for " << days << " days is " << rentalCost << endl;
break;
case 3:
MostExpensiveIndex = 0;
for (size_t carIndex = 0; carIndex < carAmount; ++carIndex) {
if (carLib[carIndex].price <= mostExpensive) continue;
mostExpensive = carLib[carIndex].price;
MostExpensiveIndex = carIndex;
}
const car & carI = carLib[MostExpensiveIndex];
cout << " Most Expensive car is: " << MostExpensiveIndex << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n";
break;
}
return 0;
}
void menu()
{
cout << " 1 - Show Cars\n";
cout << " 2 - Rental Cost\n";
cout << " 3 - Most Expensive Car\n";
}
I study C ++ and try to create the first game. Here's the code:
#include <iostream>
#include <string>
using namespace std;
void info () {
int LVL = 1;
int money = 1000;
int EXP = 0;
int work = 200;
int learn = 15;
int k = 0;
}
void menu ()
{
info ();
char menu_items;
cout << "Choose action: " << "\n" << "1. Work" << "\n" << "2. Learn" << endl;
cin >> k;
if (k == 1){
int money = money + work;
cout << "U worked (+ "<< money << " dollars)" << "\n" << endl;
} if (k == 2) {
int EXP = EXP + learn;
cout << "U learned (+ " << EXP << " EXP)" << "\n" << endl;
} else {
cout << "ERROR" << endl;
}
}
int main()
{
info ();
while (LVL == 10) {
cout << "End game!";
}
while (LVL != 10) {
cout << "Your data: " << "\n" << "Money: " << money << "\n" << "EXP: " << EXP << "\n" << "LVL: " << LVL << "\n" << endl;
menu ();
}
}
Please correct the following:
1) The cyclic output data after rewrite
2) Proper cycle add money and experience when choosing one of the following actions
Info should probably be a class or struct. You only want to instantiate it once, and persist the values over your calls.
One option might be:
#include <iostream>
#include <string>
using namespace std;
struct info {
int lvl = 1;
int money = 1000;
int exp = 0;
const int work = 200;
const int learn = 15;
};
int main()
{
info i;
string k;
while (i.lvl < 10)
{
cout << "Your data: " << "\n" << "Money: " << i.money << "\n" << "EXP: " << i.exp << "\n" << "LVL: " << i.lvl << endl;
cout << "Choose action: " << "\n" << "1. Work" << "\n" << "2. Learn" << endl;
cin >> k;
if (k == "1")
{
i.money += i.work;
cout << "You worked (+ " << i.work << " dollars, now " << i.money << ")" << endl;
}
else if (k == "2")
{
i.exp += i.learn;
cout << "You learned (+ " << i.learn << " EXP, now " << i.exp << ")" << endl;
}
else
{
cout << "ERROR" << endl;
}
}
cout << "You won!" << endl;
}
#include <iostream>
#include <string>
using namespace std;
void menu()
{
int money = 1000;
int EXP = 0;
int work = 200;
int learn = 15;
int k = 0;
cout << "Choose action: \n 1. Work \n 2. Learn "<< endl;
cin >> k;
if (k == 1){
money =money + work;
cout << "U worked (+ " << money << " dollars)" << "\n" << endl;
} if (k == 2) {
EXP = EXP + learn;
cout << "U learned (+ " << EXP << " EXP)" << "\n" << endl;
}
else {
cout << "ERROR" << endl;
}
}
int main()
{
int money = 1000;
int LVL = 1;
int EXP = 0;
while (LVL == 10) {
`cout << "End game!"; `
}
while (LVL != 10) {
cout << "Your data: " << "\n" << "Money: " << money << "\n" << "EXP: " << EXP << "\n" << "LVL: " << LVL << "\n" << endl;
menu();
}
return 0;
}