I am looking to remove the space from the name part of my assignment which allows users to put any name with or without space in my product list. for example "TV stand", I assume getline function will help me with it but I couldn't add the getline function into my main. can anyone help me with it?
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
struct product {
char product_name;
int no_of_purchase;
int no_of_sales;
double purchase_cost;
double selling_price;
double profit_loss;
double percent_profit_loss;
string product_sales;
};
bool Compare(product P1, product P2)
{
return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
int n;
cout << "Enter the number of the product:";
cin >> n;
cout << "\n";
product Products[n];
for (int i = 0; i < n; ++i) {
cout << "Enter the name of the product: ";
cin >> Products[i].product_name;
cout << "Enter the number of " << Products[i].product_name << " purchased: ";
cin >> Products[i].no_of_purchase;
cout << "Enter the number of " << Products[i].product_name << " sold: ";
cin >> Products[i].no_of_sales;
To be honest I don't understand your question but this might be helpful for you
I changed the product_name data type from char to string because the char data type is not making any sense
#include <iostream>
#include <string>
using namespace std;
struct product {
string product_name;
int no_of_purchase;
int no_of_sales;
double purchase_cost;
double selling_price;
double profit_loss;
double percent_profit_loss;
string product_sales;
};
bool Compare(product P1, product P2)
{
return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
int n;
cout << "Enter the number of the product:";
cin >> n;
cin.ignore();
cout << "\n";
product Products[n];
for (int i = 0; i < n; ++i) {
cout << "Enter the name of the product: ";
getline(cin, Products[i].product_name);
cout << "Enter the number of " << Products[i].product_name << " purchased: ";
cin >> Products[i].no_of_purchase;
cout << "Enter the number of " << Products[i].product_name << " sold: ";
getline(cin, Products[i].no_of_sales);
cin.ignore();
}
return 0;
}
Related
I'm new to c++ so my current code might be completely wrong.
I'm trying to get two inputs into two arrays I've declared two arrays of max size 10 and I want to use the console input and add it to the end of each array.
#include <iostream>
int main() {
char playerName[10];
char playerScore[10];
char name, score;
cout << "Enter the player name:";
cin >> name;
cout << "Enter the player score"
cin >> score;
for (i=0; i<10, i++)
{
// add name at the end of playerName
// add score at the end of playerScore
}
return 0;
}
First off, use string not char if you want multiple characters. You also need to initialize i first.
Then you can add inputs at the end of your array like this;
int main() {
string playerName[10];
int playerScore[10];
for (int i=0; i<10; i++)
{
cout << "Enter the player name: ";
cin >> playerName[i];
cout << "Enter the player score: ";
cin >> playerScore[i];
}
}
If you want to use character arrays, they should be 2d arrays, such as char playername [10][10].
Maximum name of 10 characters.
#include <iostream>
using namespace std;
int main() {
char playerName[10][10];
int playerScore[10];
for (int i=0; i<10; i++)
{
cout << "Enter the "<<i+1<<"th "<< "player's name:";
cin >> playerName[i];
cout << "Enter the " <<i+1<<"th "<< "player's score:";
cin >>playerScore[i];
cout << "_________________________________________\n";
}
return 0;
}
1- you must use using namespace std; after #include <iostream>
2- you must use ; at the end of cout << "Enter the player score"
3- you must specify the type of i when you use for loop and use ; between each part -> for (int i = 0; i < 10; i++)
and for fill the character array use -> cin >> playerName; and cin >> playerScore;
#include <iostream>
using namespace std;
int main() {
int n = 2;
char playerName[10];
char playerScore[10];
char name, score;
for (int i = 0 ; i < n; i++)
{
cout << "Enter the player name: ";
cin >> playerName;
cout << "Enter the player score: ";
cin >> playerScore;
cout << playerName <<'\n';
cout << playerScore << endl;
}
return 0;
}
But it is better to use a string
#include<iostream>
using namespace std;
int main()
{
int age;
char name;
cout<<"Enter age and name: ";
cin >> age >> name;
cout <<endl <<"your age: "<< age << endl << "name is: "<< name;
return 0;
}
What a run looks like:
Use a string instead of a char. A character only gets the first letter of the input.
The code should look like this.
#include<iostream>
using namespace std;
int main() {
int age = 0;
string name = "";
cout<<"Enter your age and name: ";
cin >> age >> name;
cout << endl;
cout << "Your age is " << age << endl;
cout << "Your name is " << name << endl;
return 0;
}
If you are required to use a character, you can try using a vector.
#include <iostream>
#include <vector>
using namespace std;
vector<char> _myStr;
void DisplayList () {
cout << "Your name is: ";
for (int i = 0; i < _myStr.size(); i++) {
cout << _myStr[i];
}
cout << endl;
}
void ConvertToVector (string theStr) {
for (int i = 0; i < theStr.length(); i++) {
_myStr.push_back(theStr[i]);
}
}
int main () {
int age = 0;
string name = "";
cout << "Enter your age and name: ";
cin >> age >> name;
cout << endl;
ConvertToVector (name);
cout << "Your age is: " << age;
DisplayList ();
return 0;
}
Use string instead of char...
#include<iostream>
#include <string>
using namespace std;
int main()
{
int age;
string name;
cout<<"Enter age and name: ";
cin >> age >> name;
cout <<endl <<"your age: "<< age << endl << "name is: "<< name;
return 0;
}
I have a project where I need to create a shopping list with checkout functionality. I am trying to create an array using a users input. They supply how many products they are purchasing and I need to use that to define the size of the array.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct shopList {
double pluCode;
string product;
int saleType; // 0 = per unit, 1 = per pound
double price;
double inventory;
};
int main(){
char line[255];
const int items = 0;
int n;
ofstream outfile;
outfile.open("products.txt");
cout << "How many items in your checkout: ";
cin >> items;
shopList shop[items];
for (n = 0; n < items; n++) {
cout << "Enter the PLU code: ";
cin >> shop.pluCode;
outfile << shop.pluCode << " ";
cout << "Enter product name: ";
cin >> shop.product;
outfile << shop.product << " ";
cout << "Enter the price type (0 for per unit, 1 for per pound): ";
cin >> shop.saleType;
outfile << shop.saleType << " ";
cout << "Enter the price of the product: ";
cin >> shop.price;
outfile << shop.price << " ";
cout << "How much will you purchase: ";
cin >> shop.inventory;
outfile << shop.inventory << " " << "\n";
}
outfile.close();
ifstream infile;
infile.open("products.txt");
infile.getline(line, 255);
cout << line << endl;
}
It's possible , you just have to change your declaration like that;
int items = 0 ;
cin >> items;
shopList *shop = new shopList [items];
Everything seems to check out in my code; I've checked syntax on the different functions and everything seems to be checking out but I still seem to be getting the error 'No Matching Function for Call to 'MyInput'' in XCode with no explanation. Any help would be appreciated! Code follows.
#include <iostream>
#include <iomanip>
using namespace std;
typedef char ShortName[10];
typedef char LongName[17];
typedef char IDarray[6];
void MyInput(ShortName [],char [], LongName [], float [],int [],IDarray [],int&);
int main(int argc, const char * argv[])
{
const int max = 7;
const int ConstStateTax = .05;
const int ConstFedTax = .15;
const int ConstUnionFees = .02;
ShortName firstname[max];
char MI[max];
LongName lastname[max];
float hourrate[max];
int OTHours[max];
float Gross[max];
float Overtime[max];
float GGross[max];
float StateTax[max];
float FedTax[max];
float UnionFees[max];
IDarray EmployeeID[max];
float Net[max];
MyInput(firstname,MI,lastname,hourrate,OTHours,EmployeeID,max);
return 0;
}
void MyInput(ShortName firstname[],char MI[],LongName lastname[],float hourrate[],int OTHours[],IDarray EmployeeID[],int &max)
{
for(int i = 0;i<=max;i++)
{
cout << "Please enter the first name of employee #" << i+1 << ": ";
cin >> firstname[i];
cout << "Please enter the middle initial of employee #" << i+1 << ": ";
cin >> MI[i];
cout << "Please enter the last name of Employee #" << i+1 << ": ";
cin >> lastname[i];
cout << "What is the ID number of Employee #" << i+1 << "? (6 letters or numbers only): ";
cin >> EmployeeID[i];
cout << "What is the hourly rate of employee #" << i+1 << "? ";
cin >> hourrate[i];
while (hourrate[i] < 0)
{
cout << "Invalid rate, please try again: ";
cin >> hourrate[i];
}
cout << "How many overtime hours does employee #" << i+1 << " have? ";
cin >> OTHours[i];
while(OTHours[i]<0 || OTHours[i] >20)
{
cout << "Invalid Overtime Hours, please re-enter: ";
cin >> OTHours[i];
}
}
}
the function you are trying to call is, which takes int& as last argument
void MyInput(ShortName [],char [], LongName [], float [],int [],IDarray [],int&);
but you pass const int max to it, which have type of const int and can't convert to int&
to fix it, change int& to int for both method declaration and definition
Ok, so I've made a database type program and created an item number and price field. I've also tried to stick to a primary key by using the index values of the array of both the "item number" field and "price". But was wondering how to add an "Item name" field along with this to make it work. I've tried to think of many different ways of adding a char type array but that doesn't really work.
#include<iostream>
using namespace std;
class shop
{
private:
int i,n, item[20];
float price[20];
public:
void input();
void output();
};
void shop::input()
{
cout<<"Enter the number of items: ";
cin>>n;
for (i = 1; i <= n; i++)
{
cout << "Enter the item number of the " << i << " item: ";
cin >> item[i];
cout << "Enter the price of the item: ";
cin >> price[i];
}
}
void shop::output()
{
for (i = 1; i <= n; i++)
{
cout << "Item Number: " << item[i] << endl;
cout << "Price: " << price[i] << endl << endl;
}
}
void main()
{
class shop s;
s.input();
s.output();
}
Create a class or struct to hold the data:
struct Item
{
int id;
float price; // float may lead to rounding errors.
// I would use int and store cents
//string name;
// If you really want to use c-style string
char name[20];
// or
// char *name; // You would need to use new with this
};
Then keep an array of these:
class shop
{
private:
Item items[20];
// As mentioned, better than a static array would be a vector
//std::vector<Item> items;
public:
void input();
void output();
};
What about a vector of strings?
#include<iostream>
#include <string>
#include <vector>
using namespace std;
class shop
{
private:
int i,n, item[20];
float price[20];
vector<string> names;
public:
void input();
void output();
};
void shop::input()
{
cout<<"Enter the number of items: ";
cin>>n;
names.resize(n);
for (i = 1; i <= n; i++)
{
cout << "Enter the item number of the " << i << " item: ";
cin >> item[i];
cout << "Enter the price of the item: ";
cin >> price[i];
cout << "Enter the name of the item: ";
cin >> names[i];
}
}
void shop::output()
{
for (i = 1; i <= n; i++)
{
cout << "Item Number: " << item[i] << endl;
cout << "Price: " << price[i] << endl << endl;
cout << "Name: " << names[i] << endl << endl;
}
}
And it would be good to have a destructor to clean the memory taken by the vector.
I would use std::string
#include <iostream>
#include <string>
using namespace std;
class shop
{
private:
int i,n, item[20];
float price[20];
string name[20];
public:
void input();
void output();
};
void shop::input()
{
cout<<"Enter the number of items: ";
cin>>n;
for (i = 1; i <= n; i++)
{
cout << "Enter the item number of the " << i << " item: ";
cin >> item[i];
cout << "Enter the name of the item: ";
cin >> name[i];
cout << "Enter the price of the item: ";
cin >> price[i];
}
}
void shop::output()
{
for (i = 1; i <= n; i++)
{
cout << "Item Number: " << item[i] << endl;
cout << "Item Name: " << name[i] << endl;
cout << "Price: " << price[i] << endl << endl;
}
}
int main()
{
class shop s;
s.input();
s.output();
}