No operator ">>" matches these operands [closed] - c++

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 5 years ago.
Improve this question
I've done some digging on this question and have found other people with similar, but non-identical errors to me. My two top theories are that I'm missing something obvious or I've broken Visual Studio. The code runs as follows:
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int child;
int adult;
int costs;
string movie;
int profits;
std::cout >> "What is the name of the movie? ";
std::getline(cin, movie);
std::cout >> "How many kids went to the movie? ";
std::cin << child;
std::cout >> "how many adults went to the movie? ";
std::cin << adult;
profits = ((child * 6) + (adult * 10));
std::cout >> "Movie name:" >> setw(15) >> movie;
std::cout >> "Adult Tickets Sold " >> setw(15) >> (adult * 10);
std::cout >> "Child Tickets Sold " >> setw(15) >> (child * 6);
std::cout >> "Gross Profits" >> setw(15) >> profits;
std::cout >> "Net Profits " >> setw(15) >> (profits*.2);
std::cout >> "Amount paid to distributor " >> setw(15) >> (profits - (profits*.2));
return 0;
}
Every instance of >> and << are red underlined with the error messages:
No operator '>>' matches these operands
Identifier 'setw' is undefined
I'm quite sure that I've done something glaringly obvious and wrong, but I can't find it for the life of me.

You got >> and << reversed. << is for std::cout and >> is for std::cin. You are doing the opposite. Also you need to include iomanip for std::setw.

<< is a stream insertion operator, this is used with an ostream object which is cout. >> is a stream extraction operator, which is used with and istream object cin. In your program you have clearly exchanged their places. Fix it, and then everything will work smooth.
Moreover, you have written the statement, using namespace std, then there is no need to use specify the namespace again. I mean either change std::cout (and all other similar lines) to cout or just remove the line using namespace std;. However, the latter is a better choice.

Related

Why the result differs when I press enter at the end of a sentence? [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 6 years ago.
Improve this question
Here is my first program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a;
string s;
double d;
while(cin >> a >> s >> d)
cout << a << s << d;
return 0;
}
When I input some simple data and press Enter , the result is shown immediately:
However, the code in another program behaves differently:
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double price = 0.0;
void Print();
};
void Sales_data::Print(){//print every record of the Sales_data
cout << "The bookNo of the book is " << bookNo << endl;
cout << "The units_sold of the book is " << units_sold << endl;
cout << "The price of the book is " << price << endl;
}
int main()
{
Sales_data book;
while(cin >> book.bookNo >> book.units_sold >> book.price);
book.Print();
return 0;
}
When I run this code, input some data, and press Enter, it waits for me to input more data rather than show the result.
Could you explain this to me?
Remove the semicolon after the while loop. As it is, it forces the loop to have no body, which means it just cycles over the cin forever. Even better, use braces to delimit the body:
while(cin >> book.bookNo >> book.units_sold >> book.price) {
book.Print();
}

Accelerated C++ (Section 3.2.1: vector<double> homework) [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 7 years ago.
Improve this question
In section 3.2.1 - Storing a collection of data in a vector on page 42 of the book Accelerated C++, I came across an error in my code after following what it had told me to type.
// revised version of the excerpt
double x;
vector<double> homework;
// invariant: homework contains all the homework grades read so far
while (cin >> x)
homework.push_back(x);
I understand the concept of vectors, but I simply don't understand why my code is giving me an error messages specifically pointing at the
vector<double> homework;
declaration. Does C++11 and C++14 not support this declaration for vectors anymore?
Here is my exact code:
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <ios>
#include <string>
using std::cin; using std::string;
using std::cout; using std::setprecision;
using std::endl; using std::streamsize;
int main()
{
// ask for and read the student's name
cout << "\n Please enter your first name: ";
string name;
cin >> name;
cout << " Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
cout << " Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// ask for the homework grades
cout << " Enter all your homework grades, "
" followed by end-of-file: ";
//the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
vector<double> homework;
/*invariant:
we have read COUNT grades so far, and
SUM is the sum of the first COUNT grades*/
while (cin >> x) {
homework.pushback(x);
}
// write the result
streamsize prec = cout.precision();
cout << " Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}
std::vector resides in the header <vector>. In order to use it that header needs to be included in your code. To do that you need to #include <vector>. Then you will also need to have a using std::vector; or use std::vector.

C++ Issue with Stream Operators

I'm very new to Cpp and am having some issues declaring input streams. I keep getting an error, "error: no match for 'operator>>' in 'std::cin >> new_contact.Person::Name', in addition to a whole slew of other things that all relate back to misuse of the >> operator it looks like. Here is the code, as well as the structure for each of the structs being used.
#include <iostream>
#include <map>
#include <string>
#include <vector>
using std::string;
using std::vector;
using std::endl;
using std::map;
using std::cout;
using std::cin;
typedef struct {
string Name;
map<string,int> Number;
} Person;
void addContact(vector<Person> Contacts) {
Person new_contact;
char ans;
string name;
cout << "What is the name of your new contact (first name)? " << endl;
cin >> new_contact.Name;
cout << "Do you want to enter a number for your contact? " << endl;
cin >> ans;
while (ans == 'y') {
cout << "What type of number are you entering? " << endl; //ex CELL, FAX, HOME, WORK, etc.
cin >> name;
cout << "What is the number of your contact? " << endl;
cin >> new_contact.Number[name];
cout << "do you want to enter another number? " << endl;
cin >> ans;
}
Contacts.push_back(new_contact);
}
Please let me know where I am going wrong as I do not see any glaring issues when comparing what I have with previous errors people have run into (most people experiencing this issue seem to put the endl command at the end of the cin stream, however, I have not done this). Thanks in advance!
EDIT: Now, I am stuck with an infinite loop while trying to recheck the 'ans' variable. The function simply infinitely loops through alternating "Do you want to enter another nummber?" and "What type of number are you entering?" with no input from me.
cin >> new_contact.Name requires an overload of operator>> for vector<char>, which doesn't exist.
The simplest fix is to change the type of the Name member:
string Name;

What is wrong with this C++ 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 8 years ago.
Improve this question
I'm new to programming and I've decided to try and make a Calculator that can do stuff other than simple Arithmetic. I have not finished yet, I was just testing to see if it was working so far. As I ran it, and went through Arithmetic by pressing 1 it just stops. Can someone please tell me what Ive done wrong? Thank you.
#include <iostream>
using namespace std;
int main()
{
int frsnum
int secnum
int arithchoice;
int answer;
int x;
cout << "Welcome to the advanced calculator!" << endl;
cout << "What are you trying to calculate: Simple Arithmetic < 1 >" << endl;
cout << " Systems of Equations < 2 >" << endl;
cout << " Matrices < 3 >" << endl;
cin >> x;
if(x == 1)
{
cout << "Add <1>|Subtract <2>|Multiply <3>|Divide <4>";
cin << arithchoice;
}
if(arithchoice == 1)
{
cout << "Whats the first number: "
cin >> frsnum;
cout << "And the second number: "
cin >> secnum;
answer = frsnum + secnum;
cout << "That would be: " answer << endl
}
system("PAUSE");
return 0;
}
The arrows in this statement are incorrect.
cin << arithchoice;
should be replaced by this statement
cin>> arithchoice;
Update
The best way to remember which arrows to use with Cin and Cout is that with when inputing value you are pointing from outside to the computer.
Similarly for cout you throw values from the computer to outside world.
So now if you want to pass values from real world to computer you which arrow will you use >> cin
Similarly for giving results from computer to Real world(user) "<<"
----------------
| |
Real world | <--- computer |
|_______________|
The first thing I've noticed is that in the (x==1) if block, the arrows of the cin are the wrong way round.

(cin >> int).get() doesn't properly work in Xcode(4.3.3)

I'm currently working on the book "C++ Primer Plus" and doing some of the programming excersis.
As it seems, I'm having a problem with Xcode(4.3.3) because following code doesn't work how it's supposed to work:
#include <iostream>
#include <string>
struct car
{
std::string maker;
int year;
};
int main()
{
using namespace std;
cout << "How many cars do you wish to catalog? ";
int nCars;
(cin >> nCars).get();
car* aCars = new car[nCars];
for (int i = 0; i < nCars; i++)
{
cout << "\nCar #" << (i + 1) << endl;
cout << "Please enter the make: ";
getline (cin, (aCars + i)->maker);
cout << "\nPlease enter the year made: ";
(cin >> (aCars + i)->year).get();
}
cout << "Here is your collection: \n";
for (int i = 0; i < nCars; i++)
{
cout << (aCars + i)->year << " " << (aCars + i)->maker << endl;
}
delete [] aCars;
return 0;
}
The problem is, I don't have the chance to enter any maker. The program directly goes to the point where I have to enter the year, even though I'm using "(cin >> nCars).get();" to get rid of the newline character.
Am I overlooking something?
Thanks in advance!
I suspect that you may be running on windows and the two-byte newlines are hitting you. You may be able to improve things (for lines that aren't ridiculously long) with ignore:
cin >> nCars;
cin.ignore(1024, '\n');
Note that since you rely on stream numeric processing, entering a non-numeric year such as QQ will result in the programming just finishing without asking for any more input.
You don't need to do math on the years so treat them as strings instead of integers. Then if you need to you can do validation of each year after you get the input.
Ok, guys..I found the problem.
The console within Xcode doesn't work as expected when using cin.get().
I tried the same code in the terminal as well as with Visual Studio (Win 7) and the program works perfectly.
Anyway, thank you all for your advices. I'll try consider them the next time. :)
Cheers!