i am making a project in clr windows form using c++ and im having problem in retrieving the data from the file in a template(vector) when the form is loaded. my form has the button to save the code which calls the save method in the class coded as below. pls suggest what should be the code to retrieve the file, thank you.here record isthe name of the vector.
int men_database::save(int count)
{ ofstream out;
out.open("MALE.txt",ios::out|ios::binary);
if(!out)
return -1;
else
{for(int i=0;i<count;i++)
{out<<'\n'<<record[i].getid();
out<<'\n'<<record[i].getname();
out<<'\n'<<record[i].getsize();
out<<'\n'<<record[i].getcolor();
out<<'\n'<<record[i].getprice();
out<<'\n'<<record[i].getpic();
out<<'\n'<<record[i].getwatch();
}
out.close();
}//else ends
return 1;}
Here is some basic code for reading/writing. Though you might find something better elsewhere. \n is used as delimiter. Each record should have 2 fields (in yours it is 7 fields). Each field must be single-line string. Ps, you get more attention with c++ tag on your question.
int main()
{
{
ofstream f("data.txt");
f << "id1" << endl;
f << "name1" << endl;
f << endl;
f << "id2" << endl;
f << "name2" << endl;
f << endl;
}
{
ifstream f("data.txt");
string id;
string name;
while (f)
{
f >> id;
f >> name;
if (!f) break;
cout << "id: " << id << endl;
cout << "name: " << name << endl;
cout << endl;
}
}
return 0;
}
Related
I am trying to use a while loop to calculate the average of 3 inputted grades, but I can not enter the next grade as the loops keep on going without giving me the chance to enter the next grade.
#include<iostream>
using namespace std;
int main()
{
int grade = 0;
int count = 0;
int total = 0;
cout << "Enter grade: ";
cin >> grade;
while (grade != -1)
{
total = total + grade;
count = count + 1;
cout << "Enter next grade: ";
cin >> grade;
}
int(average) = total / 3;
cout << "Average: " << int(average) << endl;
system("pause");
}
I tested your code with integer and it works fine.
If you only take int as input, the best is to put something to check the input type. Use cin.fail() to check if user input anything other than int.
for example:
while(cin.fail()) {
cout << "Error" << endl;
cin.clear();
cin.ignore(256,'\n');
cout << "Please enter grade:"
std::cin >> grade;
}
which I refer from https://www.codegrepper.com/code-examples/cpp/how+to+check+type+of+input+cin+c%2B%2B
and here as well Checking cin input stream produces an integer
std::vector<std::string> voters;
void LogVoters()
{
if (voters.size() > 0)
{
std::string out;
out.append("\n"); // newline at start
std::cout << "Vote results: ";
for (auto v : voters)
{
char formated_string[256];
std::snprintf(formated_string, sizeof(formated_string), "%s \n", v.c_str());
out.append(formated_string);
}
std::cout << out.c_str();
voters.clear();
}
}
int main() { // this gets called multiple times, thats why in voters string vector will be more names
std::string player_name = "IdontNeedName1337"; // some random name
int vote_option = 0;
char formated_string[256];
std::snprintf(formated_string, sizeof(formated_string), "%6s voted %6s", player_name.c_str(), !vote_option ? "YES." : "NO.");
voters.push_back(formated_string);
LogVoters();
}
output is like
IdontNeedName1337 voted YES.
ElonUsk102 voted NO.
I want
IdontNeedName1337 voted YES.
ElonUsk102 voted NO.
I want to center it like this, please help
edit: this is how it works
so please help me god
"
It looks like your post is mostly code; please add some more details." anoying
That code can't produce that output. Let me make a minimal reproduction of the code you posted to start with:
std::snprintf(formatted, 256, "%6s voted YES", "IdontNeedName1337");
std::cout << formatted;
std::snprintf(formatted, 256, "%6s voted NO", "ElonUsk102");
std::cout << formatted;
This would produce the result
IdontNeedName1337 voted YES.
ElonUsk102 voted NO.
Which isn't what you say you're getting, since it only has one space before "voted". You say you're getting
IdontNeedName1337 voted YES.
ElonUsk102 voted NO.
To get that, your minimal code must be something closer to
std::snprintf(formatted, 256, "%s voted YES", "IdontNeedName1337");
// ^ change here
std::cout << formatted;
std::snprintf(formatted, 256, "%s voted NO", "ElonUsk102");
std::cout << formatted;
Which isn't what you say you have in your code, so I'm largely guessing here.
I'm not sure what you thought the %6s did in the code, since that's basically the answer
std::snprintf(formatted, 256, "%20s voted YES", "IdontNeedName1337");
// ^ change here
std::cout << formatted;
std::snprintf(formatted, 256, "%20s voted NO", "ElonUsk102");
std::cout << formatted;
This tells the formatter to make this entry at least 20 characters wide. By default, it will right-pad with space characters, which matches your desired output.
You could also use std::left, std::setw, std::setfill with a stringstream object to format as per your desired output. The sample code and output are given below-
std::string player_name = "IdontNeedName1337"; // some random name
int vote_option = 0;
stringstream str;
str << std::left << std::setw(20) << std::setfill(' ') << player_name << std::setw(10) << "voted" << std::setw(10) << (!vote_option ? "YES." : "NO.") << endl;
cout << str.str(); // prints formated string
str.str("");
player_name = "ElonUsk102";
vote_option = 1;
str << std::left << std::setw(20) << std::setfill(' ') << player_name << std::setw(10) << "voted" << std::setw(10) << (!vote_option ? "YES." : "NO.") << endl;
cout << str.str(); // prints formated string
I've slowly been building up my banking system and need help. First though a quick run through. 1. choose an existing account or new one. 2. after that it will display the options of withdraw, deposit or close account (close account is the least of my worries as of right now). I've been using two different text documents as well. One is "Accounts" where it will scan or add accounts the other is "Money" where it should keep track of money in each account. The question is how can I assign the money to each account?
I've head of struct arrays, however it is confusing me. If that is the best course of action I'm going to need a mini walkthrough by making comments on the coding for me to better understand it.
#include <iostream>
#include <fstream>
#include <iomanip>//used to move text
#include <string>
using namespace std;
int display(char& anwser);
void N_account(char& anwser, string& name);
int Exist(string& name_search, char& anwser, string name_from_file, char&
anwser2, string& money_deposit);
void deposit(string& money_deposit, char& anwser, char& anwser2, int& money_D);
void withdraw(string& withdraw_money, char& anwser, char& anwser2, int& money_D, string& total_money, int& money_W);
int main()
{
int start_money, money_D, money_W;
string name, name_search, name_from_file, money_deposit, withdraw_money, total_money;
char anwser, anwser2;
display(anwser);
if (anwser == '1')
{
N_account(anwser, name);
}
if (anwser == '2')
{
Exist(name_search, anwser, name_from_file, anwser2, money_deposit);
}
if (anwser2 == '1')
{
deposit(money_deposit, anwser, anwser2, money_D);
}
if (anwser2 == '2')
{
withdraw(withdraw_money, anwser, anwser2, money_D, total_money, money_W);
}
}
int display(char& anwser)
{
cout << setw(65) << "=================" << endl;
cout << setw(65) << "Banking Managment" << endl;
cout << setw(65) << "=================" << endl;
cout << setw(60) << "1.New account" << endl;
cout << setw(65) << "2.Existing account" << endl;
cin >> anwser;
return 0;
}
void N_account(char& anwser, string& name)
{
ofstream outfile;
outfile.open("Accounts.txt", std::ofstream::out | std::ofstream::app);
cout << "Enter in first and last name for new account:";
cin.ignore();
getline(cin, name);
outfile << name;
outfile << endl;
cout << "Account added" << endl;
outfile.close();
}
int Exist(string & name_search, char& anwser, string name_from_file, char& anwser2, string & money_deposit)
{
ifstream infile;
infile.open("Accounts.txt");
cout << "Enter in your account:";
cin.ignore();
getline(cin, name_search);
while (getline(infile, name_from_file))
{
if (name_from_file == name_search)
{
cout << "Account found: " << name_search << endl;
cout << "Choose what you would like to do" << endl;
cout << setw(56) << "1.Deposit" << endl;
cout << setw(57) << "2.Withdraw" << endl;
cout << setw(62) << "3.Close account" << endl;
cin >> anwser2;
return 0;
}
}
infile.close();
}
void deposit(string & money_deposit, char& anwser, char& anwser2, int& money_D)
{
ofstream O_file;
O_file.open("Money.txt", std::ofstream::out | std::ofstream::app);
cout << "Enter in how much you would like to deposit: ";
cin.ignore();
getline(cin, money_deposit);
money_D = stoi(money_deposit);//converts string into integer so that I may use basic operators such as less than or subtraction ect.
if (money_D < 0)
{
cout << "Error!" << endl;
system("pause");
exit(1);
}
O_file << money_deposit;
O_file << endl;
O_file.close();
}
void withdraw(string & withdraw_money, char& anwser, char& anwser2, int& money_D, string & total_money, int& money_W)
{
ofstream O_file;
O_file.open("Money.txt", std::ofstream::out | std::ofstream::app);
cout << "Enter in how much you would like to withdraw:";
cin.ignore();
getline(cin, withdraw_money);
money_W = stoi(withdraw_money);//converting string to integer
total_money = money_W - money_D;
O_file << total_money;
O_file << endl;
O_file.close();
}
When I choose the account named "John Doe" with a balance of $45.00 and wish to withdraw $20.00 I want the money for John Doe to decrease to $25.00 and NOT make a new line in the "Money" text document saying $20.00 like it has been doing.
The existing accounts are:
John Doe
Jane Doe
Travis Scott
William Smith
Patrick Michaels
Courtney Desmond
Their bank accounts are:
45.00
98.00
48.00
56.00
120.00`
In this Book object I created a system to keep track of the time a book has been checked out... there is a tm* called dateCheckedOut to store the date that the book is checked out.
int Book::getHeldTime()
{
time_t now ;
time(&now);
tm* t = localtime(&now);
double difference = difftime(now, mktime(dateCheckedOut))/(60 * 60 * 24);
cout << dateCheckedOut->tm_mon << dateCheckedOut->tm_mday << dateCheckedOut->tm_year << endl; //prints out 231117, which is correct.
cout << t->tm_mon << t->tm_mday << t->tm_year << endl; //prints out 34117, which is also correct
cout << difftime(now, mktime(dateCheckedOut)) << endl; //prints out 0
cout << difference << endl; //prints out 0;
return (int)(difference); //returns 0
}
I am quite confused because I checked the date when its checked out and the date when it is loaded, both are correct, but the difftime function just returns 0. Is there anything that might cause this code to not work? Thank in advance!
P.S. dateCheckedOut only has tm_mday, tm_mon, and tm_year set to the correct value, the rest are all not set. Is that a problem?
I'm working on a mini-compiler and I reached the code generation level.
I want to store a string dynamically to the heap segment, so I wrote this C++ code (cg is the file I'm generating assembly code to) :
int length = strlen("abc");
cg << "\tori\t$a0,$0," << (length + 3) * 4 << endl; // reserve space for type + length + null + size
cg << "\tori\t$v0,$0,9" << endl;
cg << "\tsyscall" << endl;
increamentSP();
cg << "\tsw\t$v0,0($sp)" << endl;
cg << "\tori\t$t1,$0,1" << endl; // store the type
cg << "\tsw\t$t1,0($v0)" << endl;
cg << "\tori\t$t1,$0," << length << endl; // store the length
cg << "\tsw\t$t1," << 4 << "($v0)" << endl;
for (int i = 0; i < length; i++)
{
cg << "\tori\t$t1,$0," << (int)p->val[i] << endl; // store the char
cg << "\tsw\t$t1," << (2 + i) * 4 << "($v0)" << endl;
}
cg << "\tsw\t$0," << (2 + length) * 4 << "($v0)" << endl;
Basically, what I'm trying to do is :
First, I want to store a flag (1) referring that this type is a string to the first location.
Then, I want to store the size of my string to the second location.
After that, I want to store my strings chars to the next locations.
Finally, I want to store a null-terminating char to the last location.
My problem is that when I try to print my string using a code like this :
la $a0,8($t0)
ori $v0,$0,4
syscall
The result is that mips prints only the letter 'a', how can I print a string stored like this? or is there any better way to store my string?
p.s. I know I can use .asciiz in the .data segment, but the problem is that in my code I might edit the string, so I don't exactly know what my string would become.
Can any one help me with that?