c++ : How to ask a file name and print char Array [closed] - c++

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.

Related

When I compile and run; which it does perfectly fine; it is not outputting anything to the file named "Warframe_Weapons_Sheet." in the code [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 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/

how to get more than 1 word input from cin [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 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);

Redirecting to a webpage in c++ [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 5 years ago.
Improve this question
I am making a program in C++, in which a web link is there so that when i type an input or simply press enter it will redirect me to that link which i have entered.
For example if i have 3 options and i choose option A, the program will redirect me to that link which is in the option A.
Here's a sample:-
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
system("COLOR B1");
int choice;
cout << "Menu" << endl;
cout << "1. Pasta" << "\n";
cout << "2. Cold Drink" << "\n";
cout << "Your choice (1-2)" << "\n";
cin >> choice;
if(choice == 1)
{
cout << "Thanks" << "\n"; //Here i want a url and when i choose 1 it
//will direct me to that url
}
else if(choice == 2)
{
cout << "Thanks" << "\n"; // And here also...
}
else
{
return 0;
}
}
Please help.
Thanks
In Windows desktop programs you can use the ShellExecute API with the open operation to open a URL with the default application (generally a web browser).
ShellExecute(NULL, L"open", L"https://example.com", nullptr, nullptr, SW_SHOWNORMAL);
Store apps can not use ShellExecute at all, but you can use the UWP LaunchUriAsync.
task = Windows::System::Launcher::LaunchUriAsync(
ref new Windows::Foundation::Uri("https://example.com"));
The various other platforms often have their own API's. In general you want to find and use them and not to assume any particular browser executable is on the path preventing users from using another or installing to a non-default location (or getting broken by updates, etc.).

File won't open for ifstream [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
Hi my professor posted this example to her website giving an example of ifstreams, how come I cant open any .txt file?
#include <iostream>
#include <iomanip> // for setw
#include <fstream> // for ifstream, ofstream
using namespace std;
int main()
{
char filename[25]; // a string for filename entry
int val; // for reading integers from file
int sum = 0, count = 0;
double average;
ifstream in1; // create an input file stream
do
{
in1.clear();
cout << "Please enter the name of the input file.\n";
cout << "Filename: ";
cin >> setw(25) >> filename;
in1.open(filename);
if (!in1)
cout << "That is not a valid file. Try again!\n";
} while (!in1);
// PROCESS THE INPUT FILE
// Read all integer values and compute average
while (!in1.eof()) // while not end of file
{
in1 >> val; // read an integer from file
if (!in1.fail()) // in case last call failed to read an int
{ // due to trailing white space at end of file
count++;
sum += val;
}
}
average = static_cast<double>(sum) / count;
cout << "There were " << count << " numbers in the file\n";
cout << "Sum = " << sum << "\t\tAverage = " << average << "\n\n";
in1.close();
return 0;
}
This is extremely aggravating! Is it a problem with my computer or something?
Blockquote
Let me make two assumptions: you are using some IDE and you are using relative paths.
IDEs often execute your binary from a directory different than the project main dir. Try using absolute paths, find the right directory or run the file yourself.
The first thing you should start do do, is to write code to understand the error. It's not only for you now, to debug, but also for users later on when they will encounter problems:
....
if (!in1) { // replace this bloc
cout << filename << " is not a valid file\n"; // print filename to find out any issues (truncated name, etc...)
cout << "Error code: " << strerror(errno)<<endl; // Get some system info as to why
char cwd[512]; // print current working directory.
getcwd(cwd, sizeof(cwd)); // in case your path is relative
cout << "Current directory is " << cwd << endl;
cout << "Try again !\n";
}
Please note that getcwd() works as under linux, but in windows, you'll have to use _getcwd() instead.
IMPORTANT REMARK:
The following is not causing your error, but it might cause problems later on:
while (!in1.eof()) { // while not end of file
in1 >> val; // read an integer from file
...
prefer the following:
while (in1 >> val) { // while read of file works
...
Browse arround on SO: there are lots of questions/answers that explain why.

error expected ';' before 'endl' [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 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;