String not turning blue and only reading up to space - c++

Here's my actual code, it's skipping the getline(cin,phrase) completely..
maybe there's something else wrong with it, but I can't really find anything wrong.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ofstream outFile;
string file_name;
string phrase;
int number;
cout << "What file would you like to write into? ";
cin >> file_name;
outFile.open(file_name);
if(!outFile)
{
cout << "Error, could not find file. Press enter to self destruct. " << endl;
return -1;
}
cout << "What would you like to write? ";
getline (cin, phrase);
cout << "How many times? ";
cin >> number;
while(number != 0)
{
outFile << phrase;
number = number - 1;
}
system("pause");
return 0;
}

cin would read as far as whitespace is not there in your input string. To get around that you can use
getline( cin, cookie ).
OR
char input[100];
cin.getline(input,100);
EDITED AS TO CLARIFY:-
Show me the output for this :-
#include <iostream>
#include <string>
using namespace std;
int main()
{
string cookie;
getline ( cin, cookie );
cout << cookie;
system("pause");
return 0;
}

The >> operators read formatted input in term of "space separated items", so
aaaaa bbbb 123
are actually three distinct elements. The first two can be string, the third can be a string or whatever numeric scalar type.
If you want to consider spaces as part of the read, the proper function shold be getline

Related

Reading coordinate file with ifstream while ignoring headers and writing to array

There's a series of coordinates I'm trying to write to an array so I can perform calculations on, but I haven't been able to read the file correctly since I can't ignore the headers, and when I do remove the headers it also doesn't seem to correctly write the values to the array.
The coordinate file is a txt as below.
Coordinates of 4 points
x y z
-0.06325 0.0359793 0.0420873
-0.06275 0.0360343 0.0425949
-0.0645 0.0365101 0.0404362
-0.064 0.0366195 0.0414512
Any help with the code is much appreciated. I've tried using .ignore to skip the two header lines but they don't seem to work as expected.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main() {
int i = 1;
int count = 1;
char separator;
const int MAX = 10000;
int x[MAX];
int y[MAX];
int z[MAX];
int dist[MAX];
char in_file[16]; // File name string of 16 characters long
char out_file[16];
ifstream in_stream;
ofstream out_stream;
out_stream << setiosflags(ios::left); // Use IO Manipulators to set output to align left
cout << "This program reads a series of values from a given file, saves them into an array and performs calculations." << endl << endl;
// User data input
cout << "Enter the input in_file name: \n";
cin >> in_file;
cout << endl;
in_stream.open(in_file, ios::_Nocreate);
cout << "Enter the output file name: \n";
cin >> out_file;
cout << endl;
out_stream.open(out_file);
// While loop in case in_file does not exist / cannot be opened
while (in_stream.fail()) {
cout << "Error opening '" << in_file << "'\n";
cout << "Enter the input in_file name: ";
cin >> in_file;
in_stream.clear();
in_stream.open(in_file, ios::_Nocreate);
}
while (in_stream.good) {
in_stream.ignore(256, '\n');
in_stream.ignore(256, '\n');
in_stream >> x[i] >> separator >>y[i] >> separator >> z[i];
i++;
count = count + 1;
}
cout << x[1] << y[1] << z[1];
in_stream.close();
out_stream.close();
return 0;
}
Within your reading of the file, you are using in_stream.ignore(256, '\n'); correctly, but you want to use it outside the while loop. When you have it inside the while loop, every time it runs, you will ignore the first two lines, then read the third. Your output would actually read in only a third of what you expect. To fix this, just move those 2 lines outside the while loop.
in_stream.ignore(256, '\n');
in_stream.ignore(256, '\n');
while (in_stream.good)
{
in_stream >> x[i] >> separator >>y[i] >> separator >> z[i];
i++;
count = count + 1;
}
This should fix your problem, but you should generally use a vector instead of an array. Vectors automatically manage memory and check for bounds instead of you having to do that.
Also, good practice is to read values out of the stream as the while condition instead of in_stream.good:
while(stream >> var)
{
//Your code here
}
Here is a good resource on why that is.

How to make string variables more than one word c++?

When I am trying to store more than one word in a string variable, it only outputs one word when I tell the program to print it. This is an example:
#include <iostream>
using namespace std;
string i;
int main() {
cout << "Input more than one word." << endl;
//in this case the user will input whats up//
cin >> i;
cout << i << endl;
//the program outputs 'whats'//
}
Instead of using cin >> i, use getline(cin, i).
The difference is that, with getline() you get all the words in a line, whereas with operator>> you get only one word at a time.
Replace that:
cin >> i;
for:
getline(cin, i);
and it will work :)

Fix spacing in copycat program

Here is my code for a basic copycat program that just copys whatever the user types:
#include <iostream>
using namespace std;
#include <string>
int main()
{
cout << "type something.. I dare you..." << endl;
for (;;)
{
string usrin;
cout << "You: ";
cin >> usrin;
cout << "Me: " << usrin;
}
return 0;
}
But when the user inputs more than one word i get this:
Me: more
You: than
You: Me: one
You: Me: word
You:
any and all help is appreciated! thank you!
You need to use cin.getline(usrin) instead of cin >> usrin.
cin >> usrin stops reading when it finds whitespace characters in the stream but leaves the rest of the stream for the next time cin is used.
cin.getline will read until the end of the line. However, you will need to change usrin to an array of char.
char usrln[MAX_LINE_LENGTH];
where MAX_LINE_LENGTH is a constant that is bigger than the length of the longest line you expect to see.
After each input, \n leaved behind in input buffer and read on next iteration. You need to flush your input buffer. Use
cin.ignore(MAX_INT, '\n'); //Ignores to the end of line
Add <limits.h> header.
#include <iostream>
#include <limits.h>
#include <string>
using namespace std;
int main()
{
cout << "type something.. I dare you..." << endl;
for (;;)
{
string usrin;
cout << "You: ";
cin >> usrin;
cout << "Me: " << usrin ;//<<endl;
cin.ignore(INT_MAX, '\n');
}
return 0;
}

not able to store value in text file using fstream

i am new to c++ programming i am not able to store in a text file.It is a very simple program. i was storing the values using the same method earlier i was getting results.
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ofstream fout("one.txt",ios::in);
int val1, rel1;
char val2[20], rel2[20];
cout<<" \n enter the integer value";
cin>>val1;
cout<<" \n enter the string value ";
cin>>val2;
fout.close();
ifstream fin("one.txt");
fin>>rel1;
fin>>rel2;
cout<<"the integer value .\n"<<rel1;
cout<<"the string value .\n"<<rel2;
fin.close();
if (fout==NULL) {
cout<<"the file is empty";
}
return 0;
}
input
100
name
Absurd output is
the integer value is 32760
the string value is 00Dv0
There's a number of assumptions here that seem to need clarification.
If you want to write to the file you need to use fout << rel1;
You (usually) can't compare an object against NULL like you do in if(fout == NULL). This works in C# and Java because in those languages all objects are actually references, in C++ you specify when you want an object and when you want a reference.
You specify that you want to use fout to read from the file and not write to it, "ios::in".
I was a bit bored waiting for some tests to finish so I wrote how I would've written that program:
#include<iostream>
#include <string>
#include<fstream>
int main() {
std::ofstream fout("one.txt",std::ios::out);
int val1, rel1;
std::string val2, rel2;
std::cout <<"enter the integer value: ";
std::cin >>val1;
std::cout <<"enter the string value: ";
std::cin >>val2;
fout <<val1 <<" " <<val2;
fout.close();
std::ifstream fin("one.txt", std::ios::in);
if(!fin.good()) {
std::cout <<"Failed to open file\n";
return 1;
}
fin >>rel1;
fin >>rel2;
std::cout <<"the integer value: " <<rel1 <<"\n";
std::cout <<"the string value: " <<rel2 <<"\n";
fin.close();
return 0;
}

How to read string, char , int all from one file untill find eof in c++?

What's wrong with my code? I want to get intput from file (first one string, then a char , then int). I want it for whole file. Here is my code. This is giving me so pain. What can i do? Please help me.
//file handling
//input from text file
//xplosive
#include<iostream>
#include<fstream>
using namespace std;
ifstream infile ("indata.txt");
int main()
{
const int l=50;
//string t_ques;
char t_ques[l];
char t_ans;
int t_time_limit;
while(!infile.eof())
//while(infile)
{
infile.getline(t_ques,l);
//infile >> t_ans ;
infile.get(t_ans);
infile >> t_time_limit;
cout << t_ques << endl;
cout << t_ans << endl;
cout << t_time_limit << endl;
}
return 0;
}
my indata.txt file contain
what is my name q1?
t
5
what is my name q2?
f
3
what is my name q3?
t
4
what is my name q4?
f
8
out put should be the same.
but my while loop don't terminate.
A number of things:
eof checking isn't appropriate (most of the time). Instead, check stream state
don't use read as it won't skip whitespace
after your timelimit, ignore input until the end of the line
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream infile ("indata.txt");
std::string t_ques;
char t_ans;
int t_time_limit;
std::getline(infile, t_ques);
while (infile >> t_ans >> t_time_limit)
{
cout << t_ques << endl;
cout << t_ans << endl;
cout << t_time_limit << endl;
infile.ignore();
std::getline(infile, t_ques);
}
}
See it live on Coliru
Try to use this expression:
infile.open("indata.txt", ios::in);
// ...same loop...
infile >> t_ques >> t_ans >> t_time_limit;
// At the end close the file
infile.close();