BenRichi_ BenRichi_ - 13 days ago 6
C++ Question

C++ input and output stream error in xcode

So I am trying to build a simple program with C++, I am a complete beginner to C++ so try and keep it simple please!

This is my code:

#include <iostream>

using namespace std;

int main()
{
char lastName[30];
int previousBalance;
int payments;
int charges;

cout >> "Please enter your last name:\n";
cin << lastName;

cout >> "Please enter your previous balance:\n";
cin << previousBalance;

cout >> "Please enter payments since previous balance:\n";
cin << payments;

cout >> "Please enter your charges since previous balance:\n";
cin << charges;

return 0;
}


XCode gives me the same error for each cin and cout statement:

Invalid operands to binary expression ('ostream' (aka 'basic_ostream') and 'const char *')

and

Invalid operands to binary expression ('istream' (aka 'basic_istream') and 'char *')

Anything that I'm doing wrong in my code or is it something I'm doing wrong in XCode?

Answer

change your

cout >> "Please enter your last name:\n";
cin << lastName;

to

cout << "Please enter your last name:\n";
cin >> lastName;

same goes for the others

cout >> calls operator>> function which doesnt accept string. same goes for cin thats why you get that error

Comments