I'm using Visual Studio Community 2017 to run the following code but i get an error. Can anyone tell me what is the problem ?
C++ Code :
#include <iostream>
using namespace std;
int main()
{
int a, b;
while ((cin >> a >> b) != EOF)
{
cout << "Sum is: " << a + b << endl;
}
return 0;
}
Most likely issue is the EOF - the >> operator returns a reference to a stream object - not return an integer like EOF
just using tis may work ok - it will continue until a fail bit is set.
while (cin >> a >> b)
Related
I am attempting use ifstream to extract two numbers from a file in argv[1], named "inputFile", and the extracting operator seems to be extracting the bits of code rather than the numbers needed.
inputFile.txt was put into the command line operator by right clicking the project, going to properties -> debugging -> command arguments -> typing inputFile.txt into command arguments in visual studio 2017.
The file inputFile.txt is as below:
1 2
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
//Test opening file
cout << "Input file: " << argv[1] << endl;
ifstream in(argv[1]);
if (!in)
{
cerr << "Unable to open " << argv[1] << " for input";
return 1;
}
//extract numbers
int num1;
int num2;
in >> num1 >> num2;
cout << num1 << endl << num2 << endl;
in.close();
return 0;
}
I expect the int num1 to hold 1, and the int num2 to hold 2, but instead each variable holds the number -858993460.
Are you sure the file you are reading has got the data you expect? This code works fine for me, compiling with Visual Studio 2005. But, If I change the contents of the file, writing not numbers (for example if I write: a b), num1 and num2 ends with -858993460.
Could anyone help me to upgrade my code ?
I bet the function could be done in shorter and more elegant way, could you show me the proper direction or examples how to write that?
Thanks for the replies, here's the code ( written in Codeblocks 16.01):
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int D,a,b,bb,liczba,dlugosc;
int main()
{
cout << "liczba testow: ";
cin >> D;
while(D<11 && D>0){
for (int j=0;j<D;j++){
cin >> a >> b;
if(a<0 | a>1000000000 | b<0 | b>1000000000){
break;
}
liczba =a;
for(int i=1;i<b;i++){
liczba = liczba*a;
}
char aa[10] = {0};
itoa(liczba,aa,10);
string bb = aa;
int dlugosc = bb.length();
cout << bb << endl;
cout << bb[dlugosc-1] << endl;
a = 0;
}
return 0;
}
return 0;
}
I have two processes: t1.cpp and t2.cpp.
t1.cpp and t2.cpp are simplified ,I want to describe the problem easily.
//t1.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "hello\n"
<< "world\n"
<< "ok ok\n";
return 0;
}
//t2.cpp
#include <iostream>
#include <limits>
using namespace std;
int main()
{
string str;
while(getline(cin,str)){
cout << str <<endl;
}
//cin.clear();
//flush the cin
//cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
char x;
cin >> x;
return 0;
}
After compiling t1.cpp and t2.cpp. I execute them in this way ./t1 | ./t2.
Problems occur! cin >> x; in t2.cpp failed! I have no chance to type from the keyboard.
It seems the pipe command implements by redirecting the STDIN_FILENO. Does it forbid the standard input simultaneously?
My harsh requirements is obtain data from the output of t1 with shell command |,in addition,I want interact with users in t2.For example,I would display Sure to del?[y/n],and wait users's anwser.
Actually, there is something you can do in the C++ code. t1 has to read from stdin and echo that to stdout, which is redirected. You can do something like this at various places:
T value;
std::cin >> value;
std::cout << value;
Or to emulate the behaviour of the shell command in the comments (append stdin after t1 has finished writing its data to stdout):
std::copy(std::istreambuf_iterator<char>(std::cin),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(std::cout));
Finally,we deal this problem using "/dev/tty"
FILE *file = fopen("/dev/tty","r");
if(NULL == file){
/*error*/
}
int ch = fgetc(file);
if('y' == ch || 'Y' == ch){
/*balabala*/
}
when stdin or stdout was redirected , we also can read or write from the /dev/tty.
This question already has answers here:
Why isn't cin >> string working with Visual C++ 2010? [closed]
(2 answers)
Closed 5 years ago.
This is my code (created in Visual Studio 2017):
#include<iostream>
using namespace std;
int main() {
string ime;
int visina;
double tezina;
cout << "Unesite ime:" << endl;
cin >> ime;
cout << "Unesite visinu (cm):" << endl;
cin >> visina;
cout << "Unesite tezinu (kg):" << endl;
cin >> tezina;
return 0;
}
Afther the compiling process I got a this message:
no operator ">>" matches these operands
This message corresponding with this line of code: cin >> ime;
What is wrong in this code?
You should
#include <string>
I have a very strange problem. Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\n');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
This code doesn't end when I push Enter at all; therefore there is no way to complete the input. The output will stop like this and wait:
what is your name?
However if I change '\n' to 'p' or whatever char, it will finish the input when the specific char is input. For instance:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\p');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
Screen:
what is your name?
hello, Frank, how are you today?
RUN SUCCESSFUL (total time: 2s)
PS:
I am using NetBeans IDE 8.0 and Windows 8 Pro x64
New Line character depends on the operating system.
Reminder - \r\n or \n\r?
I would leave out the third parameter of the getline call all together. This will use getline's default behavior and terminate the read on the enter key.
EDIT: After reading you are still having problems, I decided to run some tests on your code.
First, to confirm that the default behavior works without specifying the third parameter to the getline method.
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
while(true){
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name);
cout << "hello, " << user_name << ", how are you today?" << endl;
}
}
The output:
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
Second, to test manually specifying the escape sequence '\n' i did this:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
while(true){
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\n');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
}
And the output:
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteBoardDev
hello, WhiteBoardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
compiled on g++ w/osx so i didnt bother testing the '\r\n' case. This brings up a good point about cross platform compatibility. If you want your code to work in multiple environments try to not specify anything platform specific in your code (aka the EOL sequence) and leave out that third parameter entirely.
Now I have solved my question with the help of WhiteboardDev. In my coding environment, I should use '\r' and during the input I need push Enter and Space, two keys in order to complete the it.