replace first line then append to end fstream c++ [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 days ago.
Improve this question
Hello I have a textfile structured as so
number of orders // replace here
<order num> <order name>
<order num> <order name>
<order num> <order name>
//append here
I simply want to append a new order to the end and then replace the num of orders by adding 1.
When I run the following code the num of orders is increased by 1 and the new appended order is not appended at the end but rather replaces the last order how can I fix this in c+++
ofstream fileTwo;
fileTwo.open("orders.txt");
fileTwo << this->numOrders;
fileTwo.open("orders.txt", ios::app);
fileTwo << endl << orderNum << " " << name << " " << sizeReal << " " << quan;
fileTwo.close();

Related

I cant seem to get the amount of decimals right using setprecision c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
I'm currently getting into subroutines/subprograms or whatever you call them in english and in this specific assignment that i'm trying to solve I have to calculate the average length of two words.
void Length(string const text_1,
string const text_2,
int & total_length,
double & mean_length)
{
total_length = text_1.length() + text_2.length();
mean_length = static_cast<double>(total_length) / 2;
}
void Length_Program(int val)
{
string text_1;
string text_2;
int total_length{};
double mean_length{};
cout << "Mata in två ord: ";
cin >> text_1 >> text_2;
cout << "Totallängd: ";
Length(text_1, text_2, total_length, mean_length);
cout << total_length << endl;
cout << "Medellängd: " << fixed << setprecision(1) << mean_length;
}
I have set the precision to setprecision(1) and I assume it will only write one decimal but I keep getting two decimals.
my example is: abcd E
it should say that it is an average of 2.5 words but it says 2.51 for some reason. Can someone help me understand what i'm doing wrong?
Your problem is that you forgot << endl on your last output line. The return code shown by the OS is appended to your output. The setprecision is working just fine.

Excel to C++? Won't Work, EVERY online youtube Tut works for them, they don't work for moi [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
read Excel w/ C++ (microsoft vis studio)
See tutorial:
https://www.youtube.com/watch?v=EjJY7yA5SWw
Here's my code:
*ifstream FILE("USE THIS FILE.xls");
//
string date; string companyTicker; string companyName; int year1; int til; int month; int year2; int asdf;
while(FILE>> date >> companyTicker >> companyName >> year1 >> til >> month >> year2 >> asdf)
{
std::cout << date << ", " << companyTicker <<
", " << companyName << ", " << year1 << ", " << til << ", " << month << ", " << year2 << ", " << asdf << endl;
}*
I've also gone on another tutorial where istream::getline is asked, of course this will cause an error "no instance of overloaded function". nothing online has been able to diagnose that issue.
Thanks all!
The tutorial you linked to shows a text file with fields separated by whitespace , which is how stream input in C++ is delimited. An XLS file is in a proprietary binary format, and cannot be directly read (without a library specifically designed for reading XLS files. Save your data as a tab-delimited file if you want to easily read it from C++.

How to use string with phrases that contain spaces [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I used cin.getline after cin.ignore() but I am getting an error saying unassigned int... Not sure what to do or what is wrong. Any suggestions?
Here is my code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string phras;
cout << " Provide a phrase, up to 30 characters with spaces > " << endl;
cin.ignore();
cin.getline(phras, sizeof(phras));
cout << " The phrase is: " << phras << endl;
cout << endl;
return 0;
}
UPDATE
I changed cin.getline(phras, sizeof(phras));
to getline(cin,phras)
Problem solved! Thanks for the help everyone!
The problem is that
char letter[1];
isn't large enough. If a C string is to hold up to N characters, it needs to be declared char letter[N+1] to allow room for the null terminator character. So if the user is going to type a single character, it needs to be:
char letter[2];
As a result, you're getting undefined behavior when cin >> letter writes 2 characters into the array that only has room for 1.
Similarly, if the user is allowed to type a 10-letter word, it should be:
char word[11];
and it should be:
char phrase[31];

Undeclared Identifier in second function? (C++) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am having an issue with my C++ code. It keeps telling me Undeclared Identifier in the second function (the menu), but I cannot see the issue since I am passing the variable. The code is here
//Menu
void menu(int &selction) //TYPO!
{
cout << "Welcome to the math work along program!" << endl;
cout << "This will generate 2 numbers with the selected operator and\n allow you to solve the equation" << endl;
cout << "1. Addition\n2. Subtraction\n3. Multiplication\nOr type -1 to quit\n";
cin >> selection;
You have a typo. You've misspelled selection as selction

Reading from .csv [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
So I am reading from the .csv as showed as the result is shown in the image url. When I'm cout-ing everything what's in the file it prints additional " " between the every read cell. So I'm wondering why are these " " cout-ed? And most importantly is there a way to get rid of them?
void Evidenca::IskanjeIstiVnos(vector<Evidenca>& evidenc, vector<Evidenca>& kopir, fstream &Datoteka){
string vrstica;
int StVrstic=0;
int Stej;
string TEMPsemester;
string TEMPletnik;
string TEMPects;
string TEMPocIzpita;
while(!Datoteka.eof()){
getline(Datoteka,vrstica,'\n');
Datoteka >> ws;
StVrstic++;
}
Datoteka.clear();
Datoteka.seekg(0, ios::beg);
cout << "Stevilo predmetov zapisanih v datoteki je: " << StVrstic<<endl; //How many lines are in file
for(int a=0;a<StVrstic;a++){
getline(Datoteka,ImePredmeta,';');
getline(Datoteka,ProfesorImPr,';');
getline(Datoteka,TEMPsemester,';');
getline(Datoteka,TEMPletnik,';');
getline(Datoteka,TEMPects,';');
getline(Datoteka,datumIzpita,';');
getline(Datoteka,TEMPocIzpita,'\n');
cout << ImePredmeta << " " << ProfesorImPr << " "<< TEMPsemester << " " << TEMPletnik << " " << TEMPects << " " << datumIzpita << " " << TEMPocIzpita << endl;
}
getline doesn't make up characters. Those characters are read from your CSV file : you can see them if you open it with a text editor (emacs, vi, notepad..), they are automatically hidden in Excel-like software.