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 9 years ago.
Improve this question
I'm facing titled problem in my assignment. Please anyone help me as quick as possible.
/*
member function of studentInfo class; store the value of all data members in
the text file named "record.txt", on separate lines in the text file.
*/
void storeFile()
{
cout<< "All the data members are stored in file." << endl;
ofstream outFile;
const char* outputFileName = "record.txt";
outFile.open(outputFileName, ios::out);
if(!outFile)
{
cout<< "\nUnable to open the file." << outputFileName << endl;
}
else
{
outFile vuID;
outFile endl;
outFile campusID;
outFile endl;
outFile studentName;
outFile endl;
outFile fatherName;
}
}
outFile endl;
This is no good. endl is an identifier in the std namespace. What are you trying to do with this? It doesn't make sense.
You need to put the variables to the outputFile
e.g.
outFile << vuID;
outFile vuID
isn't correct form, the same with
utFile endl;
It should be
outFile << vuID
and
outFile << endl;
Related
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 days ago.
Improve this question
This is my code:
int main() {
cout << "Hello, world!" << endl;
return 0;
}
// preprocessor directive#include <iostream>using namespace std;
//Main functionint main()
{
string name;
cin >> name;
cout << "hi, " << name << endl;
cout << "End Program";
return 0;
}
and it says that the bracket below the
//Main functionint main() expects a declaration
I tried moving the bracket up and down, left and right
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 1 year ago.
Improve this question
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Variables
string Weapon_Name = "undefined";
string Weapon_Type_Name = "undefined";
bool Weapon_Type_M_or_R = false;
int main(int args, char * argv[])
{
cout << "Input the weapon name." << endl;
cin >> Weapon_Name;
cout << "Confirm weapon type." << endl;
cin >> Weapon_Type_Name;
cout << "Is weapon melee or range? true or false." << endl;
cin >> Weapon_Type_M_or_R;
The if statement on line 24 as well as the code block that follows is where I first ask the boolean for melee(true) and range(false) is true, which is then followed by
me creating the file stream object "Warframe_Weapons_Sheet". I then open a .csv file I created and then input a string to the ofstream object which is followed by inputting variables that contain string values.
if (Weapon_Type_M_or_R = true) {
ofstream MyExcelFile("Warframe_Weapons_Sheet");
MyExcelFile.open("C:/Users/A12st/Downloads/Warframe_Melee_Weapons_Sheet.csv");
MyExcelFile << "Weapon Name, Weapon Type" << endl;
MyExcelFile << Weapon_Name << "," << Weapon_Type_Name << endl;
MyExcelFile.close();
} else {
ofstream MyExcelFile("Warframe_Weapons_Sheet");
MyExcelFile.open("C:/Users/A12st/Downloads/Warframe_Ranged_Weapons_Sheet.csv");
MyExcelFile << "Weapon Name, Weapon Type" << endl;
MyExcelFile << Weapon_Name << "," << Weapon_Type_Name << endl;
MyExcelFile.close();
}
return 0;
}
The line if (Weapon_Type_M_or_R = true) { should read if (Weapon_Type_M_or_R == true) {
The first is assigning a value of true to the variable and then "evaluating" that expression. The second is comparing the variable to the constant true which is what you want. Also note that if you are comparinf to true you can simply write if (Weapon_Type_M_or_R) {
You did not open any file by that name. As comments have suggested you opened "C:/Users/A12st/Downloads/Warframe_Ranged_Weapons_Sheet.csv" for writing instead. Check that file for your output.
Also keep in mind that since you did not explcitly declare it, you are using the default mode of out. Everytime your program opens that file it will be completely rewritten. To append instead, try
ofstream MyExcelFile("Warframe_Weapons_Sheet",std::ofstream::app);
Check
https://www.cplusplus.com/reference/fstream/ofstream/
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 2 years ago.
Improve this question
I am currently trying to learn C++ and I'm working on my first project.
It is supposed to ask questions and get the user to provide the input for the answer then write the input to a file with some formatting. however, I keep getting an error with the description line input, it takes the first word in a sentence and nothing else.
I've tried several things to get it to work and all of them throw a similar error or don't allow me to give any input...
cin >> description; allows me to give input of 1 word.
std::getline and getline dont allow me to give any input.
I just seem to get these errors:
Error (active) E0304 no instance of overloaded function "std::basic_istream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list
or this error:
std::basic_istream<char,std::char_traits<char>>::getline': non-standard syntax; use '&' to create a pointer to member
My current code looks like this:
int main()
{
ofstream myfile;
myfile.open("Output.txt");
if (myfile.is_open())
{
cout << "Enter kit name\n";
cin >> kit;
myfile << " \"" << kit << "\":{\n";
cout << "Default Amount?\n";
cin >> defaultamount;
myfile << " \"DefaultAmount\":" << defaultamount << ",\n";
cout << "Price\n";
cin >> price;
myfile << " \"Price\":" << price << ",\n";
cout << "Enter a Description for the kit\n";
cin >> description;
myfile << " \"Description\":\"" << description << "\",\n";
myfile.close();
cout << "output.txt has been updated with your results\n";
system("pause");
}
else
cout << "Unable to create or update text file";
return 0;
}
Assuming that you need to initialize variables and include some headers, you should include something like:
#include<fstream>
using std::ofstream;
#include<iostream>
using std::cout;
using std::cin;
#include<string>
using std::string;
string kit;
int defaultamount;
float price;
string description;
if description will have several words, you must change:
cin >> description;
for:
cin.ignore();
getline(cin,description);
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 5 years ago.
Improve this question
i am new in c++, i am working on a project, in case 1 i need to ask a file name from a user and if the file name is correct to it should print something like 6x5 character Array. I am totally confused that how to start. any single help will be appreciated.
#include <iostream>
using namespace std;
int main(){
int x;
string file;
int temp;
cout << "Welcome to Urban Heat Island Model" << endl;
cout << "What would you like to do? " << endl;
cout << "1. Load land cover file " << endl;
cout << "2. Model Temperature Based on land cover" << endl;
cout << "3. Exit " << endl;
cin >> x;
switch (x){
case 1:
cout << "What is the name of the file to import? " << endl;
cin >> file;
break;
If there is a file that you want to open, you can try a file stream. Here is a good look at how to open a file for input and output. Just make sure to check if you can actually open the file before you try to read/write from/to it. You can do it with
if(inputFile.is_open())
{
//your code here
}
If you just want to see if it is a valid file extension, check the last part of the string. If you have a list of file names, compare what they input and what you have to see if they are the same.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
OK so i know the question is a bit confusing (dont downvote right away, let me explain...)
I have a text file like this:
dim
coins
oponent
I want to read this text file but while reading it, ask the user for specific responses, for example:
"reads line with the word dim" -> asks user the dimensions -> next line -> "reads line with coins" -> asks user how many coins and so forth until the EOF.
Is there anyway to do this? if yes, can you show me how?
Thanks and plz dont downvote, just tell me what's wrong and i will edit the post..
EDIT: This is the way i'm reading the file and asking the user input
void LeitorFich::lerFicheiro(string nomeFich)
{
int i, j, t;
string linha, nome, nome1;
ifstream fich(nomeFich);
while(getline(fich, linha))
{
istringstream iss(linha);
iss >> nome;
if(nome == "dim")
{
cout << nome << " ";
iss >> i >> j;
}
}
cin.get();
fich.close();
}
A simple example will look like this:
Consider I have a file called "test.txt" which contains the very content as yours
string sLine;
ifstream inFile("test.txt");
int Dim1, Dim2, coins, oponent;
while(inFile >> sLine ){
if("dim" == sLine){
std::cout << "Dim1: ";
std::cin >> Dim1;
std::cout << std::endl;
std::cout << "Dim2: ";
std::cin >> Dim2;
std::cout << std::endl;
}
else
if("coins" == sLine){
std::cout << "coins: ";
std::cin >> coins;
}
else
if("oponent" == sLine){
std::cout << "oponent: ";
std::cin >> oponent;
}
}
inFile.close();