Recently I had to migrate one of my small turn-in projects from VS to CodeBlocks.
In VS the project worked and run just fine.
Now that I have moved my .cpp and .h to CB it runs to an error and I can't see why, because I am referring to the Menu.h.
Main:
#include <iostream>
#include "Menu.h"
#include "Bulk.h"
#include "Parcel.h"
#include <fstream>
using namespace std;
int main()
{
Menu m1 = Menu();
m1.Start();
}
Menu.cpp:
#include "Menu.h"
#include "Material.h"
#include "Bulk.h"
#include "Parcel.h"
#include <iostream>
#include <string>
#include <deque>
using namespace std;
deque<Parcel> Parcels;
deque<Bulk> Bulks;
void Menu::Start()
{
string selection = "F";
for (int i = 0; i < 1;)
{
cout << "*****Warehouse Manager*****\n";
cout << "1 - Add Material\n";
cout << "2 - List Materials\n";
cout << "3 - Add Stock\n";
cout << "4 - Decrease Stock\n";
cout << "E - Exit\n";
cin >> selection;
if (selection == "1")
{
AddMaterial();
}
if (selection == "3")
{
AddStock();
}
if (selection == "2")
{
PrintAll();
}
if (selection == "4")
{
RemoveStock();
}
if (selection == "E")
{
i++;
}
}
}
void Menu::RemoveStock()
{
string code;
double stock;
cout << "Please enter ID Code:\n";
cin >> code;
cout << "Please enter value:\n";
cin >> stock;
for (int i = 0; i < Parcels.size(); i++)
{
if (Parcels[i].GetEAN() == code)
{
Parcels[i].DecreaseStock(stock);
}
}
for (int i = 0; i < Bulks.size(); i++)
{
if (Bulks[i].GetIDCode() == code)
{
Bulks[i].DecreaseStock(stock);
}
}
}
void Menu::PrintAll()
{
for(int i = 0; i < Parcels.size(); i++)
{
Parcels[i].Print();
}
for (int i = 0; i < Bulks.size(); i++)
{
Bulks[i].Print();
}
}
void Menu::AddStock()
{
string code;
double stock;
cout << "Please enter ID Code:\n";
cin >> code;
cout << "Please enter value:\n";
cin >> stock;
for (int i = 0; i < Parcels.size(); i++)
{
if (Parcels[i].GetEAN() == code)
{
Parcels[i].IncreaseStock(stock);
}
}
for (int i = 0; i < Bulks.size(); i++)
{
if (Bulks[i].GetIDCode() == code)
{
Bulks[i].IncreaseStock(stock);
}
}
}
void Menu::AddMaterial()
{
string selection = "F";
cout << "Please choose:\n";
cout << "1 - Parcel\n";
cout << "2 - Bulk\n";
cin >> selection;
if(selection == "1")
{
string name;
string company;
string ean;
double pieceprice;
cout << "Please enter material name:\n";
cin >> name;
cout << "Please enter the company:\n";
cin >> company;
cout << "Please enter the EAN ID:\n";
cin >> ean;
cout << "Please enter the price:\n";
cin >> pieceprice;
Parcel p1 = Parcel(name, company, ean, pieceprice);
Parcels.push_front(p1);
}
if (selection == "2")
{
string name;
string company;
string idcode;
string unit;
double unitprice;
cout << "Please enter material name:\n";
cin >> name;
cout << "Please enter the company:\n";
cin >> company;
cout << "Please enter the ID:\n";
cin >> idcode;
cout << "Please enter the unit:\n";
cin >> unit;
cout << "Please enter price per 1 unit:\n";
cin >> unitprice;
Bulk b1 = Bulk(name,company,idcode,unit,unitprice);
Bulks.push_front(b1);
}
}
Menu.h:
#pragma once
#include "Material.h"
#include "Bulk.h"
#include "Parcel.h"
#include <iostream>
#include <string>
#include <deque>
using namespace std;
class Menu
{
public:
void Start();
void AddMaterial();
void AddStock();
void PrintAll();
void RemoveStock();
private:
};
The error is the following:
Do you guys have any idea what is going on? How come that it runs perfectly in VS but not in CB?
Thank you!
Related
I'm making this program using a class named animal, below is the code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "animal.h"
using namespace std;
int main() {
animal moke;
ofstream out;
out.open("/Users/Abel/Desktop/Cpp/animal/animal/animalRecords.txt");
vector<animal> monke;
bool quit = false;
while (!quit) {
int choice;
cout << "1. Add info for animals 2. Edit Info for animals 3. Exit\n";
cin >> choice;
string nam;
string spec;
int ag;
int editRow;
//menu
switch (choice) {
case 1:
cout << "Enter the Animal's name\n";
cin >> nam;
moke.setName(nam);
cout << "Enter the Species\n";
cin >> spec;
moke.setSpecies(spec);
cout << "Enter the age\n";
cin >> ag;
moke.setAge(ag);
monke.push_back(moke);
break;
case 2:
if(monke.size() == 0) {
cout << "Nothing to modify";
}
else {
for (int i = 0; i < monke.size(); i++) {
cout << i+1 << "\t\tName = " << monke[i].getName() << "\tSpecies = " << monke[i].getSpecies() << "\tAge = " << monke[i].getAge() << endl;
}
cout << "Which row would you like to edit\n";
cin >> editRow;
if (editRow > monke.size() || editRow < 1) {
cout << "Please follow directions\n";
}
else {
cout << "1. Edit name\t2. Edit species\t3. Edit age\n";
cin >> choice;
switch(choice) {
case 1:
cout << "Enter the new name\n";
cin >> nam;
monke[editRow-1].setName(nam);
break;
case 2:
cout << "Enter the new species\n";
cin >> spec;
monke[editRow-1].setSpecies(spec);
break;
case 3:
cout << "Enter the new age\n";
cin >> ag;
monke[editRow-1].setAge(ag);
break;
default:
cout << "You need to learn how to follow instruction <:(\n";
}
}
}
break;
case 3:
if (monke.size() == 0) {
cout << "Bye\n";
}
else {
for (int i = 0; i < monke.size(); i++)
out << i+1 << "\t\tName = " << monke[i].getName() << "\tSpecies = " << monke[i].getSpecies() << "\tAge = " << monke[i].getAge() << endl;
}
break;
default:
cout << "You are so basic\n";
break;
}
}
}
#pragma once
#include <string>
using namespace std;
class animal{
public:
animal();
animal(string name, string species, int age);
animal(animal &E);
void setName(string name);
void setSpecies(string species);
void setAge(int age);
string getName();
string getSpecies();
int getAge();
private:
string n, s;
int a;
};
#include "animal.h"
#include <string>
using namespace std;
animal::animal() {
n = "";
s = "";
a = 0;
}
animal::animal(string name, string species, int age) {
n = name;
s = species;
a = age;
}
animal::animal(animal &E) {
a = E.a;
s = E.s;
s = E.s;
}
void animal::setName(string name) {
n = name;
}
void animal::setSpecies(string species) {
s = species;
}
void animal::setAge(int age) {
a = age;
}
string animal::getName() {
return n;
}
string animal::getSpecies() {
return s;
}
int animal::getAge() {
return a;
}
When running this, it shows these errors:
No matching constructor for initialization of 'animal'
is_cpp17_move_insertable<std::allocator<animal>, void>::value' "The specified type does not meet the requirements of Cpp17MoveInsertable"
No matching constructor for initialization of 'animal'
I'll start by saying I have worked on this for 3 days now and this is only my second semester programming. I know this question is probably easy for most, but I really have very little experience.
The code I have written all works as intended except the invalid/blank entry validation. Anything I have found and tried just breaks other parts of the code or doesn't work at all.
Here are the instructions given in the homework for the part I am having issues with:
"Any invalid input for the menu will simply redisplay the menu.
Option 1 will prompt for a userName. An empty name will be ignored."
Here is my code. Any help is greatly appreciated.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> usernames;
void listMenu();
void addName();
void listNames();
void removeName();
int main()
{
char entry;
bool exit = false;
while (exit == false)
{
cout << "Choose from the following menu: \n";
listMenu();
cin >> entry;
if (entry == '\n')
{
listNames();
}
if (entry == '1')
{
addName();
}
else if (entry == '2')
{
listNames();
}
else if (entry == '3')
{
removeName();
}
else if (entry == 'x' || entry == 'X')
{
exit = true;
}
}
usernames.clear();
return 0;
}
void listMenu()
{
string menu[4] = { "1. Add a username","2. List all usernames","3. Delete a username","X. Exit" };
for (int i = 0; i < 4; i++) {
cout << menu[i] << endl;
}
}
void addName()
{
string name;
cout << "Enter a username: " << endl;
cin >> name;
usernames.push_back(name);
}
void listNames()
{
int n = 1;
cout << "**************\n";
for (auto& x : usernames)
{
cout << n <<". "<< x <<endl;
n++;
}
cout << "**************\n";
}
void removeName()
{
int x;
cout << "Which username would you like to remove?\n";
listNames;
cin >> x;
usernames.erase(usernames.begin()+x);
}
You can test your input and clear it if it's invalid. Using cin.fail, cin.clear and cin.ignore.
#include<iostream>
using namespace std;
bool cond;
int main() {
int n;
do {
cout << "Enter an integer number:";
cin >> n;
cond = cin.fail();
cin.clear();
cin.ignore(INT_MAX, '\n');
} while(cond);
return 0;
}
My code allows for the user to either input some numbers or read them from a document. How would I also allow the user to pick from a choice of math problems to perform on those numbers without having a bunch of code in my main.
Here is my code so far:
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//ask for file input or manual input
cout << "Press 1 to enter numbers or 2 to read them from a list." << "\n";
int choice;
cin >> choice;
if (choice == 1)
{
int numberswanted;
cout << "How many numbers would you like to enter?" << "\n";
cin >> numberswanted;
vector<double> list;
for (int i = 0; i < numberswanted; i++)
{
cout << "Enter your numbers: " << "\n";
double x;
cin >> x;
list.push_back(x);
}
}
else if (choice == 2)
{
ifstream doc;
float output;
doc.open("input.txt");
while (!doc.eof())
{
vector<double> list;
double x;
doc >> x;
list.push_back(x);
}
doc.close();
}
return 0;
}
May be you can try something like this :
int main() { cout << "Press 1 to enter numbers or 2 to read them from a list." << "\n";
int choice;
cin >> choice;
if (choice == 1)
{
choice1();
}
else if (choice == 2)
{
choice2();
}
return 0;}
when choice1 contains int [...] list.push_back(x);}
etc...
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
char response = 'y';
while (response == 'y')
do
{
const int numberofnames = 200;
vector <string> boynamevector(numberofnames);
vector <string> girlnamevector(numberofnames);
ifstream readboy;
ifstream readgirl;
string boy;
string girl;
int choice;
readboy.open("BoyNames.txt");
readgirl.open("GirlNames.txt");
bool check = true;
int boychoice = 1;
int girlchoice = 2;
int bothchoice = 3;
for (int counter = 0; counter < numberofnames; counter++)
{
readboy >> boynamevector[counter];
readgirl >> girlnamevector[counter];
}
cout << "\t\Popular Boy or Girl or Both Choices\n\n"
<< "1. Popular Boy Name\n"
<< "2. Popular Girl Name\n"
<< "3. Popular name for both Boy and Girl\n"
<< "Enter choice: ";
cin >> choice;
if (choice == boychoice)
cout << "Enter a boy's name: ";
cin >> boy;
check = false;
int counter;
for (counter = 0; counter < numberofnames; counter++);
{
if (boy == boynamevector[counter])
check = true;
}
if (check == true)
cout << "The name is popular\n";
else
cout << "The name is not popular\n";
cout << "Would you like to run the program again? \n"
<< "Enter y for yes or n for no: ";
cin >> response;
} while (response == 'Y' || response == 'y');
return 0;
}
I think the problem is in the first for loop that appears. I'm not sure that the names are being transferred from the open file to the string array/vector and I'm not entirely sure how to make that work. If anyone could help or give some suggestions that would be greatly appreciated.
You have a typo:
for (counter = 0; counter < numberofnames; counter++);
That semicolon makes the next lines be a normal block, so boynamevector[counter] is out of range.
First you need properly read all the boys and girls names BEFORE you enter the loop so you don't re-read the names over and over again:
string response = "y";
ifstream readboy;
ifstream readgirl;
//Open the files
readboy.open("BoyNames.txt");
readgirl.open("GirlNames.txt");
if(!readboy.is_open())
return 0;
if(!readgirl.is_open())
return 0;
vector <string> boynamevector;
vector <string> girlnamevector;
string szName;
while(getline(readboy, szName))
boynamevector.push_back(szName);
while(getline(readgirl, szName))
girlnamevector.push_back(szName);
readboy.close();
readgirl.close();
Then iterate over and over comparing the strings
do
{
string boy;
string girl;
int choice;
bool bExists = true;
int boychoice = 1;
int girlchoice = 2;
int bothchoice = 3;
cout << "\tPopular Boy or Girl or Both Choices\n\n"
<< "1. Popular Boy Name\n"
<< "2. Popular Girl Name\n"
<< "3. Popular name for both Boy and Girl\n"
<< "4. Exit\n"
<< "Enter choice: ";
cin >> choice;
if(choice == 4)
break;
if (choice == boychoice)
cout << "Enter a boy's name: ";
if (choice == girlchoice)
cout << "Enter a girls's name: ";
cin >> boy;
bExists = false;
//Find the name
if(choice == 3 || choice == 1)
{
for (unsigned int i = 0; i < boy.size(); i++)
{
if (boy == boynamevector[i])
bExists = true;
}
}
else if(choice == 3 || choice == 1)
{
for (unsigned int i = 0; i < girlnamevector.size(); i++)
{
if (boy == girlnamevector[i])
bExists = true;
}
}
if (bExists == true)
cout << "The name is popular\n";
else
cout << "The name is not popular\n";
cout << "Would you like to run the program again? \n"
<< "Enter y for yes or n for no: ";
cin >> response;
} while (response == "Y" || response == "y");
return 0;
I am new to C++ and I am making a program to generate a multiplication table. Here is he code.
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number to find Multiplication Table ";
cin >>num;
for(int a=1;a<=10;a++)
{
cout<<num<<" x "<<a<<" = "<<num*a<<endl;
}
cout << "Press ENTER to continue...\n";
cin.get();
getchar();
return 0;
}
I want that after the multiplication table of one number is displayed , the user should have an option of entering another number or exiting.
Like press "n" to enter a new number or "e" to exit
This may be what you want. (this is the implementation of main function)
int num;
char command;
bool exit=false;
while(!exit)
{
cout << "Enter a number to find Multiplication Table ";
cin >>num;
for(int a=1;a<=10;a++)
{
cout<<num<<" x "<<a<<" = "<<num*a<<endl;
}
cout << "Press n to continue or e to exit\n";
cin >> command;
while(command != 'n' && command != 'e')
{
cout << "Just press n to continue or e to exit!\n";
cin >> command;
}
if (command == 'e')
{
exit=true;
}else
{
exit=false;
}
}
return 0;
#include <iostream>
using namespace std;
int main()
{
int num;
char ch;
do{
cout << "Enter a number to find Multiplication Table";
cin >> num;
for(int a=1;a<=10;a++)
{
cout<<num<<" x "<<a<<" = "<<num*a<<endl;
}
cout << "Press \"n\" to enter a new number or \"e\" to exit\n";
}while(cin>>ch && ch=='n');
return 0;
}