C++ doesn't convert string from data - c++

I want to write a little program which should be used in supermarkets. everything is fictitious and it's only for learning purposes.
However, The tool generate a new data for every new article. in the data there are 2 lines, the name and the prise.
The data is named as the article number of the product. So the user enter a articlenumber and the tool looks for a data with this number, if it found it, it reads the 2 lines and initiates the variables.
But for some reasons it does not convert and copy the strings correctly.
here is the part which loads the data.
int ware::load()
{
string inhalt;
cout << "please insert article number" << endl;
cin >> articlenumber;
productname.open(articlenumber, ios::in);
if (!productname.is_open())
{
cout << "can't find the product." << endl;
return 1;
}
if (productname.is_open())
{
while (!productname.eof())
{
getline(productname, inhalt);
strcpy(name,inhalt.c_str());
getline(productname, inhalt);
price = atoi (inhalt.c_str());
cout << inhalt << endl;
}
warenname.close();
}
cout << endl << endl <<
"number: " << inhalt <<
" preis: " << price <<
" name: " << name <<
endl << endl; //this is a test and will be deleted in the final
}
hope you can help me!
Here is the class:
class ware{
private:
char articlenumber[9];
char name[20];
int price;
fstream warennamefstream;
ifstream warenname;
public:
void newarticle(); //this to make a new product.
void scan(); //this to 'scan' a product (entering the article number ;D)
void output(); //later to output a bill
int load(); //load the datas.
};
hope everything is fine now.

First, you have a using namespace std; somewhere in your code. This occasionally leads to subtle bugs. Delete it. ( Using std Namespace )
int ware::load()
{
string inhalt;
cout << "please insert article number" << endl;
cin >> articlenumber;
The type of articlenumber is incorrect. Declare it std::string, not char[]. ( What is a buffer overflow and how do I cause one? )
productname.open(articlenumber, ios::in);
There is no reason to have an ifstream lying around waiting to be used. Also, there is no point in providing ios::in -- it is the default. Just use the one-argument form of the ifstream constructor.
if (!productname.is_open())
{
cout << "can't find the product." << endl;
return 1;
}
Don't bother checking to see if the file opened. Your users don't care if the file was present or not, they care whether the file was present AND you retrieved the essential data.
if (productname.is_open())
{
while (!productname.eof())
{
getline(productname, inhalt);
strcpy(name,inhalt.c_str());
getline(productname, inhalt);
price = atoi (inhalt.c_str());
cout << inhalt << endl;
}
warenname.close();
}
This loop is just wrong.
Never invoke eof(). It doesn't do what you think it does, and will cause bugs.
Why is this a loop? Aren't there only two lines in the file?
There is no point in calling close. Just let the file close when the istream goes out of scope.
Why is warename different than productname?
Don't store your data in char[]. This is the 21st century. Use std::string.
.
cout << endl << endl <<
"number: " << inhalt <<
" preis: " << price <<
" name: " << name <<
endl << endl; //this is a test and will be deleted in the final
Never use endl when you mean to say '\n'. Each of those endl manipulators invokes flush, which can be very expensive. ( What is the C++ iostream endl fiasco? )
You forgot to return a value.
Try this instead:
int ware::load()
{
// This declaration should be local
std::string articlenumber;
cout << "please insert article number" << endl;
cin >> articlenumber;
// This declaration should be local
std::ifstream productname(articlenumber.c_str());
// These declarations can be class members:
std::string name;
int price;
std::string number;
if(getline(productname, name) &&
productname>>price &&
productname>>number)
{
cout << "\n\n" <<
"number: " number <<
" preis: " << price <<
" name: " << name <<
"\n\n"; //this is a test and will be deleted in the final
return 0;
} else {
cout << "can't find the product." << endl;
return 1;
}
}

Related

How to get variable values from main and send them to a function?

I have encountered a problem while running this code in my IDE. You can see very early on that I've attempted to use a function. The reason for this is to save memory later on by outputting text, but the problem occurs with the variables in the function. The classType variable is uninitialized, how do I prevent this? I have defined them in main, but when I try to output the text with the variables from main then it doesn't work properly.
#include<iostream>
using namespace std;
string getName()
{
   string charName;
   int classType;
   cout << "What is your " << classType << "'s name?" << endl;
   cin >> charName;
   return charName;
}
int main()
{
   int classType; //Later we will ask the user what class they're playing.  
   string charName;
   /*We will use a function to ask a question.  
   We use a function to save memory instead of copy-pasting the text*/
   cout <<"Welcome to \"Orcs and Ogres\"" << endl;
   cout << "What class do you want to play?  " << endl;
   cout << "\tType 1 for Warrior class]" << endl;
   cout << "\tType 2 for Archer class ]" << endl;
   cout << "\tType 3 for Mage class   ]" << endl;
   cin >> classType;
   if(classType == 1)
   {
       cout << endl << "You are a warrior" << endl;
       string classType;
       classType = "warrior";
       getName();
   }
   else if(classType == 2)
   {
       cout << endl << "You are an archer" << endl;
       string classType;
       classType = "archer";
       getName();
   }
   else if(classType == 3)
   {
       cout << endl << "You are a mage" << endl;
       string classType;
       classType = "mage";
       getName();
   }
   else
   {
       cout << endl << "UserError:  Number too high or too low";
   }
}
On the lines of code that use getName(), it outputs something like "What is your blank's name?" instead of the proper classType. I want to know how I can send variable values to a function from main so that it outputs text properly here.
The reason why it's not working is because your getName function has no knowledge of whats stored in the classType variable. Read on how function variable scope works to understand the whole mechanism works might be beneficial.
If you wish to keep the current implementation of your program. Re-write your getName function to accept the string class as a parameter
string getName(string classType)
{
string charName;
cout << "What is your " << classType << "'s name?" << endl;
cin >> charName;
return charName;
}
and in your main you'd call the function as follow :
getName("Warrior"); // to ask warrior for a warriors' name
getName("Mage"); // to ask for a mage's name.
You may also want to add to include the string library at the top of your file as not having it might also cause your code to simply not work. As well as making sure to properly store the name returned from your getName() function as follow :
string name = getName("Warrior");
Also , as others have said , maybe reading a bit more on how function receive and return values might be beneficial to you.
It is as simple as this. Try this updated code...
#include<iostream>
using namespace std;
string getName(string classType)
{
string charName;
cout << "What is your " << classType << "'s name?" << endl;
cin >> charName;
cout<<"your "<<classType<< "'s name is "<<charName<<endl;
return charName;
}
int main()
{
int Type;
string charName;
cout <<"Welcome to \"Orcs and Ogres\"" << endl;
cout << "What class do you want to play? " << endl;
cout << "\tType 1 for Warrior class]" << endl;
cout << "\tType 2 for Archer class ]" << endl;
cout << "\tType 3 for Mage class ]" << endl;
cin >> Type;
if(Type == 1)
{
cout << endl << "You are a warrior" << endl;
string classType;
classType = "warrior";
getName("warrior");
}
else if(Type == 2)
{
cout << endl << "You are an archer" << endl;
string classType;
classType = "archer";
getName("archer");
}
else if(Type == 3)
{
cout << endl << "You are a mage" << endl;
string classType;
classType = "mage";
getName("mage");
}
else
{
cout << endl << "UserError: Number too high or too low";
}
return 0;
}

How to iterate over string variables of a class object in C++?

So here I have a class definition of a Car and then I create a carObject with it. I want the user to input values for all the variables in the carObject. As you see here, I have managed to get user input, but my approach to this problem is inefficient in my opinion.
I notice that all of the user inputs, except for the first one are very similar. I would like to use a loop of some kind to iterate over the declaration statements, or blocks of statements, and change the variable every time. I would like to put an if statement to enter different input only for the first iteration of the loop. I know that in bash I could use a string variable to stand for the variable name, but I don't know if that's possible in C++.
Notice that the object name does not change, but only the variables that are associated with it. I also use the same word for the user input, which preferably should be changed every iteration. I also have a series of arrays which are named similarly. The purpose of these arrays is to tell the user what options are available for a particular variable.
Although I have previous programming experience, I am relatively new to C++. A block of code that would serve as a solution to my problem that involves a call to another function would suit my purposes. Here is my code below.
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
string Name;
string Model;
string Color;
string Transmission;
string Category;
};
int main() {
Car CarObject;
string modelOptions [3] = { "Ferrari", "Porsche", "Nissan" };
string colorOptions [4] = { "Blue", "Red", "Green", "White" };
string transmisionOptions [2] = { "Automatic", "Manual" };
string categoryOptions [3] = { "A", "B", "C" };
cout << "Enter " << "name" << " for Car 1." << endl;
cin >> carObject.Name;
cout << endl;
cout << "Enter " << "model" << " for Car 1." << endl;
cout << "Options are:";
for (const string &text: modelOptions) {
cout << " " << text;
}
cout << "." << endl;
cin >> carObject.Model;
cout << endl;
cout << "Enter " << "color" << " for Car 1." << endl;
cout << "Options are:";
for (const string &text: colorOptions) {
cout << " " << text;
}
cout << "." << endl;
cin >> carObject.Color;
cout << endl;
cout << "Enter " << "transmission" << " for Car 1." << endl;
cout << "Options are:";
for (const string &text: transmissionOptions) {
cout << " " << text;
}
cout << "." << endl;
cin >> carObject.Transmission;
cout << endl;
cout << "Enter " << "category" << " for Car 1." << endl;
cout << "Options are:";
for (const string &text: categoryOptions) {
cout << " " << text;
}
cout << "." << endl;
cin >> carObject.Category;
cout << endl;
...
return 0;
}
void Car::InputParameter(string& param, const string &msg, const vector<string>& options)
{
cout << msg << endl;
for (const string &text: options) {
cout << " " << text;
}
cout << "." << endl;
cin >> param;
cout << endl;
}
I think you might want something like this. You just call it for each member.
This block of code:
cout << "Enter " << "category" << " for Car 1." << endl;
cout << "Options are:";
for (const string &text: categoryOptions) {
cout << " " << text;
}
cout << "." << endl;
cin >> carObject.Category;
cout << endl;
… can be replaced with a call to a function like this:
carObject.Category = userInput( "category", categoryOptions );
Clearly it returns a string (that is, a std::string).
The options argument should better be made a vector<string>.
Then just replace the other similar blocks with ditto calls to that function.
Is it a good idea to make that function a member function of Car?
No.
Consider, for example, how to then use Car in a GUI program (Graphical User Interface).

Reading a text file and saving every line as a string (C++)

I have a little question. I'm currently doing a school assignment in c++ and the task is to have something similar to a small library, where the user can ask to look at a book and then the program will print out the release year, author, how many pages etc etc. The assignment is focused on vectors and arrays, but I thought a smart way of doing it could be to have the release years in a text file and then save those years in an array. When I first it, everything was saved in characters, (meaning "1","8","8","5"), when I'd actually like it to save every line in the text document as a string in the array, or something similar (like this: "1885",). I couldn't really figure out how to split them up into strings then. I then talked a bit to a friend and this is where I am with my code now, it's not really working and at the moment I have no idea how I am supposed to solve it. Main problem is I don't know how to read and save every line in the text document as a string, however I am grateful for any help that would make me be able to print out a single year from the text document, in any other way.
This is what my code looks like:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <istream>
using namespace std;
void book();
void readFile(int input);
void oddEven();
void stringLiner();
void factorial();
int main()
{
int input;
while (input != 0)
{
cout << "Hello. Welcome to the first, truly big, assignment in this programming course." << endl;
cout << "Which part do you wish to access?" << endl;
cout << "1. Book program" << endl;
cout << "2. 2 arrays - One EVEN ~ One ODD" << endl;
cout << "3. The one at a time string" << endl;
cout << "4. Factorial array" << endl;
cout << "0. Exit " << endl;
cin >> input;
switch (input)
{
case 1:
book();
break;
}
}
}
void book() //This is the function used to do the book thing
{
cout << string( 100, '\n' );
int input;
string year[5] = {"1883"/*Treasure Island*/ }; //Array for the years the books were written
string author[5] = {"Robert Louis Stevenson"/*Treasure Island*/, "yollo"}; //Array for the authors
string pages[5] = {"304"/*Treasure Island*/,"420" }; //Array for the number of pages
string books[5] = {"Treasure Island", "Swagolo" }; //Array for the name of the books
cout << "You have chosen to look at books." << endl;
cout << "These are the books in the library. Pick one to see what year it was written in, what author wrote it and how many pages it contains. " << endl;
cout << "These are the books in the library: " << endl;
for (int i = 0; i<5; i++) //Loop to display all the books + what number to press to access them.
{
cout << i+1 << " " << books[i] << endl;
};
cout << "Please type a number to look at that book. " << endl;
cin >> input;
int TresIsl = input-1;
switch (input) //Switch case to chose which book to look at.
{
case 1: //Info about Treasure Island
cout << "This is " << books[TresIsl] << " and this is some info. " << endl << endl;
cout << books[TresIsl] << " was released in " ;
readFile(input);
cout << " and it was written by " << author[TresIsl] << ". ";
cout << "This book contains " << pages[TresIsl] << " pages. " << endl;
break;
case 2:
cout << "This is " << books[TresIsl] << " and this is some info. " << endl << endl;
cout << books[TresIsl] << " was released in " ;
readFile(input);
cout << " and it was written by " << author[TresIsl] << ". ";
cout << "This book contains " << pages[TresIsl] << " pages. " << endl;
break;
}
}
void readFile(int input)
{
ifstream file("year.txt");
int numlines = 0;
int numMaxLines = 5;
vector<string> lines (numMaxLines);
while(numlines < numMaxLines && !file.eof())
{
getline(file, lines);
numlines++;
}
cout << lines[input];
}
The other void functions are for other tasks in this assignment which I didn't include now, I just copy pasted the code where they were included. Also please don't mind the slightly childish stuff in the code.
Also I am very sorry if this breaks any rules for the forum or something similar. I tried to find another topic like this for c++, but I couldn't find anything helpful.
It's not clear what exactly your problem is, but assuming that you want to read a file line-by-line and get a vector of those lines, something like this would do it:
std::vector<std::string> readLines(const std::string& filename)
{
std::vector<std::string> lines;
std::ifstream input(filename);
std::string line;
while (std::getline(input, line))
{
lines.push_back(line);
}
return lines;
}
if any one's still got a question, a friend and me discussed it and he helped me a bit, and we got a code that works in my case at least, so I thought I'd show it to you:
void readFile(int input)
{
ifstream file("year.txt");
string in;
vector<string> lines;
if (file.is_open())
{
while ( getline (file, in) )
{
lines.push_back(in);
}
cout << in;
}
file.close();
cout<<lines[input-1]<<endl;
}
The cout in the end I guess is unnecessary in some cases, but this worked for me and my homework. Thanks for everyone's help anyways.

Infinite loop reading file into array

I have an infinite loop while trying to read a file. File is saved from user input, then it is to be read, finally, displayed using a separate function.
Here is my read function. The loop is not finding the eof(); from the file. I can't see what the issue is. No compiler errors.
void read(HouseholdItems items[AMOUNT], fstream& myFile, fstream& yourFile)
{
myFile.open("insured.dat", ios::in | ios::binary); // Open myFile
yourFile.open("uninsured.dat", ios::in | ios::binary); // Open yourFile
for(int i = 0; i < AMOUNT; i++)
{
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
for(i = 0; i < AMOUNT; i++)
{
while (myFile)
{
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: ";
cout << items[i].insured << endl;
}
}
}
myFile.close();
yourFile.close();
}
The following loop can never end : no modification is made to myFile inside its body :
while (myFile)
{
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: ";
cout << items[i].insured << endl;
}
You have to read the file inside the while loop for it to end.
Besides, you most likely have a variable name conflict: you have two for loops that use the same variable i ; which is presumably not what you want.
Your problem is while(myFile), since nothing about myFile changes in that loop. It isn't clear what it is you mean for that loop to accomplish, so I can't say what to replace it with. (It does seem problematic that you have nested for loops, but don't seem to have a table of data.)
The answers by #ScottHunter and #Ekelog already answer the real problem. The following are peripheral problems.
These lines are not right:
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(&items[i]));
You need to use:
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
// ^^ Drop the &
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
// ^^ Drop the &
try this in while
while (!myFile.eof())
{
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: ";
cout << items[i].insured << endl;
}
See inline comments:
void read(HouseholdItems items[AMOUNT], fstream& myFile, fstream& yourFile)
{
// since you open and close those files here, you probably
// want to declare them here instead as a function parameter
myFile.open("insured.dat", ios::in | ios::binary); // Open myFile
yourFile.open("uninsured.dat", ios::in | ios::binary); // Open yourFile
// test in the loop
for(int i = 0; i < AMOUNT && myFile && yourFile; i++)
{
// these two reads do not make sense, the second one
// will overwrite the data just read by the first one...
// maybe you meant that one of the file might be smaller?
// or maybe to compare the results in some ways (in which
// case you need two arrays)
myFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
yourFile.read(reinterpret_cast <char*>(&items[i]),sizeof(items[i]));
// write current result
cout << "description: ";
cout << items[i].description << endl;
cout << "quantity: ";
cout << items[i].quantity << endl;
cout << "price: ";
cout << items[i].price << endl;
cout << "insured: ";
cout << items[i].insured << endl;
}
myFile.close();
yourFile.close();
}
As a side note:
Your second for() loop reused the i variable which means it would not work at all as expected.
As pointed out by others the sizeof() was wrong, you could also use sizeof(items[0]) since all items are equal in size.
As mentioned by SamIAm, the while() was blocking because the file was not being read so the EOF never actually reached in the file.
As shown in the comments, the file objects should probably be defined in the function instead of outside and passed in as references.

Ofstream not creating a new file correctly

I've tried to write a simple database program. The problem is that ofstream does NOT want to make a new file.
Here's an extract from the offending code.
void newd()
{
string name, extension, location, fname;
cout << "Input the filename for the new database (no extension, and no backslashes)." << endl << "> ";
getline(cin, name);
cout << endl << "The extension (no dot). If no extension is added, the default is .cla ." << endl << "> ";
getline(cin, extension);
cout << endl << "The full directory (double backslashes). Enter q to quit." << endl << "Also, just fyi, this will overwrite any files that are already there." << endl << "> ";
getline(cin, location);
cout << endl;
if (extension == "")
{
extension = "cla";
}
if (location == "q")
{
}
else
{
fname = location + name + "." + extension;
cout << fname << endl;
ofstream writeDB(fname);
int n = 1; //setting a throwaway inteher
string tmpField, tmpEntry; //temp variable for newest field, entry
for(;;)
{
cout << "Input the name of the " << n << "th field. If you don't want any more, press enter." << endl;
getline(cin, tmpField);
if (tmpField == "")
{
break;
}
n++;
writeDB << tmpField << ": |";
int j = 1; //another one
for (;;)
{
cout << "Enter the name of the " << j++ << "th entry for " << tmpField << "." << endl << "If you don't want any more, press enter." << endl;
getline(cin, tmpEntry);
if (tmpEntry == "")
{
break;
}
writeDB << " " << tmpEntry << " |";
}
writeDB << "¬";
}
cout << "Finished writing database. If you want to edit it, open it." << endl;
}
}
EDIT: OK, just tried
#include <fstream>
using namespace std;
int main()
{
ofstream writeDB ("C:\\test.cla");
writeDB << "test";
writeDB.close();
return 0;
}
and that didn't work, so it is access permission problems.
ofstream writeDB(fname); //-> replace fname with fname.c_str()
If you lookup the documentation of the ofstream constructor, you will see something like:
explicit ofstream ( const char * filename, ios_base::openmode mode = ios_base::out );
The second argument is optional, but the first one is a const char*, and not a string. To solve this problem the most simple way is to convert your string to something called a C-string (char*, which is basically an array of chars); to do that just use c_str() (it«s part of the library).
Other than that, you could just place the information directly on a C-str, and then pass it normally to the ofstream constructor.