#include<iostream>
#include<string>
using namespace std;
int main()
{
int i;
cout<<"enter ur no. plz";
cin>>i;
cout<<"ur no. is:"<<i;
cin.get();
return 0;
}
This code is not displaying the integer I entered. It returns back after entering an integer and hitting enter. I am using dev C++.
After the user enters the integer, there is still a newline character left in the input buffer. cin.get() reads that character, then the program immediately ends. You could put an additional call to get if you want the program to stay open. Or, before the call to get, you could have a call to ignore:
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
That would clear the newline character from the buffer.
Or you could run your program from the command line, you'll see the output then.
Add some endls:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int i;
cout<<"enter ur no. plz"<<endl;
cin>>i;
cout<<"ur no. is:"<<i<<endl;
cin.get();
return 0;
}
Related
#include<iostream>
#include<vector>
#include<ios>
#include<limits>
int main()
{
using namespace std;
vector<string> disliked,words;
int n;
cout<<"Enter the word that you dislike."<<endl;
for(string word;cin>>word;)
disliked.push_back(word);
cout<<"Enter the list of words."<<endl;
cin.sync();
for(string word;cin>>word;)
words.push_back(word);
for(int i=0;i<words.size();i++)
{
int n=0;
for(int j=0;j<disliked.size();j++)
{
if(disliked[j]==words[i])
n++;
}
if(n==0)
cout<<words[i]<<endl;
}
cout<<"Program completed."<<endl;
return 0;
}
Write a program to bleep out the word that you don't like.First input the list of words that you don't like.
Program terminates after printing "Enter list of words."
Instead of cin.sync() use cin.clear();
You may also need to use cin.ignore() also.
The problem is you have a ^D stuck in cin and it's blocking any future cin entries. Control D closes the system pipe. And the program immediately exits.
It might be more usable if you check for an input that ends the input list.
Execution using cin.sync():
$ ./a.out
Enter the word that you dislike.
test
a
b
c
^d
Enter the list of words.
Program completed.
$
Execution after replacing cin.sync() with adding cin.clear() and cin.ignore():
$ ./a.out
Enter the word that you dislike.
test
a
b
c
^d
Enter the list of words.
a
b
c
^d
Program completed.
$
Why I can't take inputs of a string using gets,getline and cin.getline.when I debugg it seems that compiler skips those lines.here's my code-
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s1,s2;
char *p;
int n,m,i;
cin>>n;
for(i=1;i<=n;i++)
{
int j=0;
getline (cin,s1);
getline (cin,s2);
cout<<s1<<"\n";
while(s1[j]!='\0')
{
if(s1[j]==' ')
{
s1.erase(s1[j]);
}
j++;
}
}
cout<<s1<<S2<<endl;
return 0;
}
What about j variable, it isn't set to zero when next for-loop iteration begin, so in second iteration you work with garbage.
Every time you use cin, it stores every character entered in memory until it encounters a newline character. This block of memory is called the input buffer.
when you take the input for 'n' the return key is in the cin buffer.
You should use cin.ignore to get rid of this newline.
Before getline (cin,s1); add cin.ignore statement
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 8 years ago.
Hi guys i am facing an unknown error while taking input from getline.My purpose is to take a number and two strings as input from the user and print the first string.Here is the problem code
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{ string s,p;
getline(cin,s);
getline(cin,p);
cout<<s;
}
return 0;
}
Now when i give input like:
1
abababa abb
b
it doesn’t print anything.Why is it happening?
After cin>>t, there is a newline remaining in the stream, then the newline will be assigned to s, so cout<<s seems to print nothing(Actually, it prints a newline).
add cin.ignore(100, '\n'); before first getline to ingore the newline.
Each time you use cin to get something like in cin >> t, it will leave a newline in the input buffer. so in next operation it will be affected by that and will seem to skip the "wait for return key" and hence abnormality. To avoid that usecin::ignore.
the documentation says:
Extracts characters from the input sequence and discards them, until
either n characters have been extracted, or one compares equal to
delim.
The function also stops extracting characters if the end-of-file is
reached. If this is reached prematurely (before either extracting n
characters or finding delim), the function sets the eofbit flag.
I have written your code in very understandable way but working
Let me know if you have any issue
#include <iostream>
using namespace std;
int main() {
int t=0;
cout<<"Enter t\n";
cin>>t;
cin.ignore();
while(t>0)
{ string s,p;
cout<<"Enter s\n";
getline(cin,s);
cout<<"Enter p\n";
getline(cin,p);
cout<<" Values s:"<<s<<" p:"<<p<<"\n";
t--;
}
return 0;
}
The newline from cin >> t; after pressing enter is still in std::cin when getline(cin,s) is called, resulting in s being empty. The string you enter is actually being stored in p. Try using a different capture method or flushing the cin buffer before using it again.
I think it's because getline() doesn't ignore the new line ccharacter, but it reads it in the next call yo getline. Try yo put a getline() before each calle, something like this:
Int main() {
int t;
cin>>t;
string s,p;
getline(cin,s);
while(t--)
{
getline(cin,s);
getline(cin, p);
getline(cin,p);
cout<<s;
}
Hope it helps :)
The code below is for storing a group of words in a std::vector and to count how many times a particular word given by the user appears in the vector by comparing it to all the words stored in the vector.
The console does not prompt me for input at the second std::cin >> in the program below.
#include <iostream>
#include <ios>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[])
{
cout<<"enter your words followed ny EOF"<<endl;
vector<string> words;
string x;
typedef vector<string>::size_type vec_size;
vec_size size;
while (cin>>x)
{
words.push_back(x);
}
size=words.size();
cin.clear();
//now compare
cout<<"enter your word:"<<endl;
string my_word;
int count=0;
cin>>my_word; //didn't get any prompt in the console for this 'cin'
for (vec_size i=0; i<size; ++i)
{
my_word==words[i]?(++count):(count=count);
}
cout<<"Your word appeared "<<count<<" times"<<endl;
return 0;
}
The final output I get is "Your word appeared 0 times".
What is the problem with the code. Any help would be great.
The program reads the word list until end of file. So, at a terminal, you can type the EOF character (Ctrl-D on Linux, Ctrl-Z Return on Windows), but what then?
I think after resetting the stream, a terminal will continue to read. But if the program is taking input from a disk file, pipe, etc., there is no hope. End-of-file is forever.
Instead, use some sort of sentinel, or prefix it by a count. That way the first loop can run until the logical end of the list. And then it can read the word intended for the summary logic.
while (cin>>x && x != '*') // The word "*" ends the list of words
{
words.push_back(x);
}
size=words.size();
//now compare
cout<<"enter your word:"<<endl;
while (cin>>x)
{
words.push_back(x);
}
Here, you're reading until failure. So, when this loop finishes, cin is in an error state. You need to clear the error state:
cin.clear();
http://www.cplusplus.com/forum/articles/6046/
Read this as an example and probable issues !!
Trying to get some basic understanding of console functionalities. I am having issues so consider the following...
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
/*
This is a template Project
*/
void MultiplicationTable(int x);
int main()
{
int value = 0;
printf("Please enter any number \n\n");
getline(cin, value);
MultiplicationTable(value);
getchar();
return 0;
}
I actually based this off code from http://www.cplusplus.com/doc/tutorial/basic_io/ . My IDE is not recognizing getline() so of course when I compile the application. I get an error
'getline': identifier not found
Now take a look at this code
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
/*
This is a template Project
*/
void MultiplicationTable(int x);
int main()
{
int value = 0;
printf("Please enter any number \n\n");
cin>>value;
MultiplicationTable(value);
getchar();
return 0;
}
When I execute this line of code the console window opens and immediately closes. I think I a missing something about cin. I do know that it delimits spaces but I don't know what else. what should I use for input to make my life easier.
The function getline() is declared in the string header. So, you have to add #include <string>.
It is defined as istream& getline ( istream& is, string& str );, but you call it with an int instead of a string object.
About your second question:
When I execute this line of code the console window opens and immediately closes
There is probably still a '\n' character from your input in the stream, when your program reaches the function getchar() (which I assume you put there so your window doesn't close). You have to flush your stream. An easy fix is, instead of getchar(), add the line
int c;
while((c = getchar()) != '\n'){}
This will flush your stream until the next line-break.
Remark: conio.h is not part of the c++ standard and obsolete.
The getline function reads strings, not integers:
#include <string>
#include <iostream>
using namespace std;
int main() {
string line;
getline( cin, line );
cout << "You entered: " << line << endl;
}
You are exiting the program before you can view the results because (I'm guessing) you double-clicked the .exe file from inside a Windows Explorer (or the Desktop) view in order to execute. Instead, go to Start, Run, type in cmd.exe and open a command window. Navigate to where your program resides. Type in your program's name on the command line and execute. It will stay open until you intentionally close the command window.