It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
void main (void)
{
char name [2] [30], number [2] [10];
char << "Please type your first name, a blank, and last name) << endl;
cin >> name;
cout << "Name=" <<name << endl;
cout << "Please type a number, press the return key, and another number << endl;
cin >> number [0] >> endl;
cout << number << endl;
}
Too many to mention, but we're not here to act as a homework service. Examine the output of your compiler then tackle them one at a time:
qq.cpp:4:13: warning: missing terminating " character
qq.cpp:4: error: missing terminating " character
qq.cpp:7:13: warning: missing terminating " character
qq.cpp:7: error: missing terminating " character
qq.cpp:1: error: ‘::main’ must return ‘int’
qq.cpp: In function ‘int main()’:
qq.cpp:4: error: expected unqualified-id before ‘<<’ token
qq.cpp:6: error: ‘cout’ was not declared in this scope
qq.cpp:6: error: ‘endl’ was not declared in this scope
qq.cpp:8: error: ‘cin’ was not declared in this scope
At a bare minimum:
No using clause or std:: prefixes.
char is not a stream.
No closing quotes on some of the string literals.
There is a parenthesis instead of a double quotation mark at the end of "Please type your first name, a blank, and last name)
You don't end the string with a " as in
char << "Please type your first name, a blank, and last name) << endl;
and
cout << "Please type a number, press the return key, and another number << endl;
it should be:
int main (void)
{
char name [2] [30], number [2] [10];
char << "Please type your first name, a blank, and last name)" << endl;
cin >> name;
cout << "Name=" <<name << endl;
cout << "Please type a number, press the return key, and another number" << endl;
cin >> number [0] >> endl;
cout << number << endl;
return 0;
}
char << "Please type your first name, a blank, and last name) << endl;
and
cout << "Please type a number, press the return key, and another number << endl;
are both missing end double quotes
char << "Please type your first name, a blank, and last name)" << endl;
cout << "Please type a number, press the return key, and another number" << endl
Related
I have written the following code
switch (number) {
case 1:
int accountnumber[20];
char firstname[20], lastname[20], balance[20];
cout << "please enter the account number of the user " << endl;
cin.getline(accountnumber, 20);
cout << "please enter the first name of the user " << endl;
cin.getline(firstname, 20);
cout << "please enter the last name of the user " << endl;
cin.getline(lastname, 20);
cout << "please enter the balance of the user " << endl;
cin.getline(balance, 20);
ofstream myfile(" data.txt");
myfile << accountnumber;
myfile.close();
int accountnumber1[20];
ifstrean.obj("data.txt");
obj.getline(accountnumber1, 20);
cout << "data is" << accountnumber1;
obj.close();
}
It is showing the following errors
no matching function for call to 'std::basic_istream<char>::getline(int[20], int) '
expected unqualified-id before'.' token
What are the mistakes that I am doing?
The problem is "accountnumber[20]" on line 4 is an integer array and you pass it to cin.getline on line 10, when it expects a char array instead. Since you're using a char array for numeric values such as balance, you might wanna consider making your account number into a char array as well.
int accountnumber[20];
char firstname[20],
lastname[20], balance[20];
becomes
char accountnumber[20], firstname[20], lastname[20], balance[20];
The formatting on your code is also really unconventional, you might want to stick to something more common for making it easier for anyone to read it.
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 3 years ago.
What seems to be the problem in my code?
When I hit Enter after entering my age, the prompt already finishes without asking for my address. I know I can use getline() for the age, but what if the user enters a non-integer answer?
Sorry, I just started coding yesterday and I want to learn the basics.
#include <iostream>
using namespace std;
int main()
{
int age;
string name, address;
cout << "Please enter the name of the user:" << endl;
getline(cin, name);
cout << "Please enter age of the user:" << endl;
cin >> age;
cout << "Please enter the address of the user:" << endl;
getline(cin, address);
cout << "Your name is " << name << " and you are " << age
<< " years old " << " who lives in " << address << endl;
cout << "Thank you for using my program!";
return 0;
}
Just add 'cin.ignore()' after 'cin>>age' like this:
cout << "Please enter age of the user:" << endl;
cin >> age;
cin.ignore();
cout << "Please enter the address of the user:" << endl;
getline(cin, address);
When getline() reads input, then a newline character is left in input stream, due to which it dosen't reads the string(address) in your program.
And if a user enters a 'float' or 'double' etc. instead of 'int' in age then it will simply extract out integer from it ,
for example:
if user enters 39.29 or 39.00005 or 39.00 or then age=39
To know more about getline check the following link:
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Peeps. I'm a noob. I have a question to ask but here's my code below.
int main() {
int a, c, favNum;
favNum = 3;
cout << "Hello! Please enter a number in the space below. This number will be called 'a'. " << endl;
cout << "Enter: " << endl;
cin >> a;
if ( typeid(a) == typeid(int) && cin.fail()==false){
cout << "Great Job!" << endl;
cout << "Let's do cool stuff with this program! " << endl;
* cout << "Type '1' for checking whether your number, " + a + ", is a
number divisible by," + favNum + "." << endl;
cout << "Type '2' to recieve a compliment " << endl;
cin >> c;
switch (c) {
case 1:
cout << "Awesome!" << endl;
if( a%favNum == 0 ){
* cout << "Your 'a' number, " +a+ ", is a number divisible by
'favNum'," +favNum+ "." << endl;
} else if (a%favNum != 0){
* cout << "Your 'a' number, " +a+ ", is a number not divisible by
'favNum'," +favNum+ "." << endl;
}
break();
case 2:
cout << "Great Job!" << endl;
}
} else {
cout << "Oops.. Might wanna try to write a number. Doesn't look like a
number to me.." << endl;
cout << "Please restart your program and follow instructions next
time."
<< endl;
}
Basically what my program does is it takes an input of a number 'a', and checks if it's a number and if it is, it uses "switch case" to direct me to 2 options.
Problem: It shows me this weird error "error: invalid operands of types 'const char' and 'const char [28]' to binary 'operator+'" and I have no idea what it means. It popped me this error on lines 16,23,and 25, and I asterisked the lines so you can see where the error shows up. Thanks if you can help me!
EDIT: My question WAS what is the error? and why my program is not working? I realized that I had done a + b instead of a << "+" << b. I've been working on 2-3 languages in coding, and I made an error. Just 1 day ago, I even said my question was answered. Thank you.
BTW: Program worked just fine before switch case! LOL
EDIT: Question solved.
The line
cout << "Type '1' for checking whether your number, " + a +
", is a number divisible by," + favNum + "." << endl;
Does not work since:
"Type '1' for checking whether your number, " + a does not do what you are hoping it will do.
That line is equivalent to:
const char* cp1 = "Type '1' for checking whether your number, ";
const char* cp2 = cp1 + a; // Depending on the value of a, cp2 points
// to something in the middle of cp1 or something
// beyond.
cout << cp2 + ", is a number divisible by," + favNum + "." << endl;
That is a problem since the plus operator is not defined for the type of cp2 and the string literal that follows the + operator. The error message from the compiler refers to that term.
Type of cp2 is const const*.
Type of the string literal is const char[28].
You can get what you want by using the insertion operator (<<) repeatedly.
cout
<< "Type '1' for checking whether your number, "
<< a
<< ", is a number divisible by,"
<< favNum
<< "."
<< endl;
Make sure to make similar changes to other lines that suffer from the same problem.
Hello This may be a simple question and I am sorry for consuming your time for such a simple question but I am trying to learn c++. The following is a small program I am just practicing on as I learn. I have hit a snag in the code as when I execute it I get the following error: " In function 'int main()':
20:19: error: no match for 'operator!' (operand type is 'const string {aka const std::basic_string}')
20:19: note: candidate is:
20:19: note: operator!(bool)
20:19: note: no known conversion for argument 1 from 'const string {aka const std::basic_string}' to 'bool'
Further below you will find all of my code Thank you for the help!
#include
#include
using namespace std;
int main()
{
const string go = "tomato";
string Answer;
cout << "What is your friends favorite food?" << endl;
cout << "ENTER HERE:" << " " << flush;
cin >> Answer;
if(Answer == go)
{
cout << "Congratulations you know your friend!!!" << endl;
}
while(Answer =! go)
{
cout << "What is your friends favorite food?" << endl;
cout << "ENTER HERE:" << " " << flush;
cin >> Answer;
cout << "You do not know your friend please try again!!!" << endl;
}
return 0;
}
Answer =! go means Answer = (!go). The negation of go is undefined, as go is not bool.
What you probably want is a comparison for 'not equal', which is !=. The sequence of the two characters is significant.
The inequality comparison operator is !=, and not =!.
Change your code to this (in particular change the =! to !=
using namespace std;
int main()
{
const string go = "tomato";
string Answer;
cout << "What is your friends favorite food?" << endl;
cout << "ENTER HERE:" << " " << flush;
cin >> Answer;
if(Answer == go)
{
cout << "Congratulations you know your friend!!!" << endl;
}
while(Answer != go)
{
cout << "What is your friends favorite food?" << endl;
cout << "ENTER HERE:" << " " << flush;
cin >> Answer;
cout << "You do not know your friend please try again!!!" << endl;
}
return 0;
I have been asked to create a petrol pump program for my referral coursework and I'm having an issue with running it, at the moment this is the main thing the compiler is outputting while trying to compile the code full build dialogue here
1>m:\visual studio 2010\projects\referral\referral\main.cpp(56): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <ctime>
#include <cmath>
#include <string>
#include <Windows.h>
using namespace std;
int reciept();
int pump;
int petrol;
int main()
{
bool exit = false;
int code;
string p1w ("Waiting");
string p2w ("Waiting");
string p3w ("Waiting");
string p4w ("Waiting");
string p1r ("Ready");
string p2r ("Ready");
string p3r ("Ready");
string p4r ("Ready");
if (GetAsyncKeyState(VK_ESCAPE))
{
exit = true;
}
cout << "***************************************************" << endl;
cout << "*Liquid Gold v1.0.0 Last revised 18/07/13 *" << endl;
cout << "*The process of processing transactions is simple,*" << endl;
cout << "*activate a pump by entering its code shown below.*" << endl;
cout << "*After pump operation is verified (generally 10 *" << endl;
cout << "*seconds though this may vary) the attendant *" << endl;
cout << "* will be able to enter the amount of petrol to 3 *" << endl;
cout << "*decimal places which will then be converted based*" << endl;
cout << "*on a predetermined price (which can be altered in*" << endl;
cout << "*price.txt) and once this process is complete a *" << endl;
cout << "*receipt will be created (you will need seperate *" << endl;
cout << "*software in order to print this recipt) and the *" << endl;
cout << "*transaction will be terminated. *" << endl;
cout << "*© Simple Software Solutions 2013 *" << endl;
cout << "***************************************************" << endl << endl;
system("Pause");
while (exit == false)
{
cout << " Pump (1) - " << p1w << " Pump (2) - " << p2w << endl << endl << endl;
cout << " Pump (3) - " << p3w << " Pump (4) - " << p4w << endl << endl << endl;
cin >> "Please enter a pump code:" >> code;
if (code == 1)
{
swap (p1w, p1r);
pump = 1;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 2)
{
swap (p2w, p2r);
pump = 2;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 3)
{
swap (p3w, p3r);
pump = 3;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else if (code == 4)
{
swap (p4w, p4r);
pump = 4;
cin >> "Please enter the amount of petrol deposited" >> petrol;
break;
}
else
{
cout << "Invalid pump code entered";
}
reciept();
{
ofstream transactions;
transactions.open ("reciept.txt");
transactions << "****************************************************/n";
transactions << " SALE /n";
transactions << "****************************************************/n /n";
}
}
return 0;
}
I've looked around and the only solution I can find for that error is including which I've already done and I can't see any other solution.
Anyone more observant than me care to have a look through and tell me where I'm going wrong?
Also I know my code is an inefficient mess and I apologise for that.
Change
cin >> "Please enter a pump code:" >> code;
to
cout << "Please enter a pump code: ";
cin >> code;
You need to change all the cin >> "string" in your code. This does not mean prompting user for input. Instead, you're actually trying to write into a string literal.
Just for some added color on top of Yang's answer, this is not a "binary error" as suggested in the title. The error message refers to binary'>>'. >> is a binary operator, and binary operators take two operands, one on each side. + and - are functioning as binary operators in the following:
1 + 2
var1 - var2
A unary operator takes just one operand. & and - are functioning as unary operators in the following:
my_pointer = &n;
int var3 = -5;
The important part in the error message you're getting:
binary '>>' : no operator found which takes a left-hand operand of
type 'std::istream' (or there is no acceptable conversion)
is the last bit, "or there is no acceptable conversion". There certainly is a >> operator which takes a left hand operand of std::istream, but there is no >> operator defined which takes a string literal on the right hand side, since string literals can't be assigned to. In this case, std::cin >> myvar takes stuff from std::cin and tries to put it into the variable myvar, but there's no way you can stuff anything into a string literal like "Please enter a pump code:", as that would be like trying to do:
"Please enter a pump code:" = 5;
which is obviously nonsense.