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>
Related
#include<iostream>
using namespace std;
int main()
{
int num1,num2,result;
cout << "enter num 1"<<endl;
cin >> num1;
cout << "enter num 2"<<endl;
cin >> num2;
result = num1 + num2;
cout << num1 <<"+" << num2 << " = "<< result;
return 0
}
this is the code I am unable to enter in output for adding the numbers
I wrote c++ code of adding two nums when output is given to enter 1st no then I am unable to write the no how can I write that output
The last sentence your code :"return 0",didn't write ";".
I have to write a program in C++ that asks user to enter lines and store them in a text file.
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
cout << "Enter the line you want to enter in the file:" << endl << endl;
gets(line);
fout << line << endl;
cout << "\nDo you want to enter another line?" << endl;
cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
}
The problem is that the text is not being stored in different lines.
I have to use Turbo C++ for this program.
Minimal, Reproducible Example:
#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
void show(char name[80])
{
char line[800];
cout << "Contents of the file:" << endl << endl;
ifstream fin (name);
while(!fin.eof())
{
fin.getline(line,80);
if(fin.eof())
break;
cout << line;
}
}
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
cout << "Enter the line you want to enter in the file:" << endl << endl;
fflush(stdin);
gets(line);
fout << line << endl;
cout << "\nDo you want to enter another line?" << endl;
fflush(stdin);
cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
show(name);
}
int main()
{
char name1[80];
cout <<"Enter the name of the text file:" << endl;
gets(name1);
create_file(name1);
return 0;
}
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.
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.
Hey so im making a program to figure out a energy cost my program so far is, but I want the watt-hours to be for all three appliances not just one how do i do that?:
#include <iostream>
using namespace std;
int main ()
{
double watts, hours_per_day, watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day;
cout << "How many Watts for the Television? " ;
cin >> watts;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day;
cout << "How many Watts for the Washer? " ;
cin >> watts;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day;
watt_hours = watts * hours_per_day * 30;
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
You will have to use a stand-alone watts variable and hours_per_day for each appliance.
Also consider using loops for doing such, it makes code more clearer and less sized;
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
static const double dollars_per_wh = 0.00008;
int main(int argc, char ** argv)
{
int num_apps = 3;
std::vector<std::string> v_apps;
v_apps.push_back("Air conditioner");
v_apps.push_back("Television");
v_apps.push_back("Washer");
double watt, hours, watt_hours = 0;
for(register int i = 0; i < num_apps; ++i)
{
cout << "How many watts for the " << v_apps[i].c_str() << "? ";
cin >> watt;
cout << "How many hours/day do you run the " << v_apps[i].c_str() << "? ";
cin >> hours;
watt_hours = watt_hours + ((watt * hours) * 30);
}
cout << "You have used " << watt_hours << " Watt-Hours of electricity." << endl;
return 0;
}
I guess you could do the following with triple variables:
#include <iostream>
using namespace std;
int main ()
{
double watts_ac, watts_tv, watts_wa;
double hours_per_day_ac, hours_per_day_tv, hours_per_day_wa;
double watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts_ac;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day_ac;
cout << "How many Watts for the Television? " ;
cin >> watts_tv;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day_tv;
cout << "How many Watts for the Washer? " ;
cin >> watts_wa;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day_wa;
watt_hours = (watts_ac * hours_per_day_ac + watts_tv * hours_per_day_tv + watts_wa * hours_per_day_wa) * 30; //I'm not sure what this 30 factor is due to though.
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
}
or just aggregate data:
#include <iostream>
using namespace std;
int main ()
{
double watts, hours_per_day_ac, watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day;
watts_hours = watts*hours_per_day;
cout << "How many Watts for the Television? " ;
cin >> watts;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day;
watts_hours += watts*hours_per_day;
cout << "How many Watts for the Washer? " ;
cin >> watts;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day;
watts_hours += watts*hours_per_day;
watt_hours *= 30; //I'm not sure what this 30 factor is due to though.
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
}