A lot of : "error: no match for 'operator>>' (operand types are 'std::basic_ostream<char>' and '<unresolved overloaded function types>') [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 2 years ago.
Improve this question
This is the code :
#include <iostream>
using namespace std;
int main()
{
cout << " SSSSSSSSSSSSSSS" >> endl;
cout << " SS:::::::::::::::S" >> endl;
cout << "S:::::SSSSSS::::::S" >> endl;
cout << "S:::::S SSSSSSS" >> endl;
cout << "S:::::S" >> endl;
cout << "S:::::S" >> endl;
cout << "S::::SSSS" >> endl;
cout << " SS::::::SSSSS" >> endl;
cout << " SSS::::::::SS" >> endl;
cout << " SSSSSS::::S" >> endl;
cout << " S:::::S" >> endl;
cout << " S:::::S" >> endl;
cout << "SSSSSSS S:::::S" >> endl;
cout << "S::::::SSSSSS:::::S" >> endl;
cout << "S:::::::::::::::SS" >> endl;
cout << " SSSSSSSSSSSSSSS" >> endl;
cout << "IIIIIIIIII" >> endl;
cout << "I::::::::I" >> endl;
cout << "I::::::::I" >> endl;
cout << "II::::::II" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << " I::::I" >> endl;
cout << "II::::::II" >> endl;
cout << "I::::::::I" >> endl;
cout << "I::::::::I" >> endl;
cout << "IIIIIIIIII" >>
}
This is for school so don't ask why I am writing this code. But i get a lot of errors like the one in the title and I can't seem to find the problem so if anyone can help me, it'll be great!

When writing using the extraction or insertion operator (e.g >> or << respectively) you need to basically point it where you want the flow to go, and not change the flow for that whole statement, to put it in simple terms, in your case you want both insertion/extraction operators that you use to be facing like so << and that one is called an insertion operator.
This is the code you want:
#include <iostream>
using namespace std;
int main()
{
cout << " SSSSSSSSSSSSSSS" << endl;
}
and replicate that for as many times as you want there to be output statements.

Keep the flow going one direction and one direction only:
std::cout << "..." << std::endl;
You've got this going in and out at the same time.
You can't read from std::cout, it's for output only. It's an ostream which means it has no idea what >> even is. That operator isn't defined which is why you get that error.

Related

C++ Calling a string from a struct stops the program [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 2 years ago.
Improve this question
I'm a beginner in C++. I appreciate any suggestions in which could improve my code but what I'm really looking for is an explanation as to why my current code isn't returning what I want.
#include <iostream>
#include <string>
using namespace std;
struct MATRIX {
float values[1][1];
string names;
};
MATRIX get_matrix(string name, MATRIX m){
m.names = name;
cout << "Enter values of 2x2 Matrix:" << endl;
cin >> m.values[0][0];
cin >> m.values[0][1];
cin >> m.values[1][0];
cin >> m.values[1][1];
return m;
}
// end get_matrix1
MATRIX get_matrix2(string name2, MATRIX m2){
m2.names = name2;
cout << "Enter values of 2x2 Matrix:" << endl;
cin >> m2.values[0][0];
cin >> m2.values[0][1];
cin >> m2.values[1][0];
cin >> m2.values[1][1];
return m2;
}
int main(){
string testname;
MATRIX matrixtest;
string testname2;
MATRIX matrixtest2;
cout << "Name 1st Matrix:" << endl;
cin >> testname;
MATRIX result = get_matrix(testname, matrixtest);
cout << "Name 2nd Matrix:" << endl;
cin >> testname2;
MATRIX result2 = get_matrix2(testname2, matrixtest2);
cout << "[" << result.values[0][0] << "," << result.values[0][1] << "]" << endl;
cout << "[" << result.values[1][0] << "," << result.values[1][1] << "]" << endl << endl;
cout << "[" << result2.values[0][0] << "," << result2.values[0][1] << "]" << endl;
cout << "[" << result2.values[1][0] << "," << result2.values[1][1] << "]" << endl << endl;
}
It's supposed to return the name of the matrix as well as its values. Whenever it hits the name member in result it just stops the program.
EDIT: Updated to current code
You have to specify the number of elements, not the maximum index, to declare arrays in C++.
With this declaration
float values[1][1];
Only values[0][0] is avaliable.
The declaration should be
float values[2][2];

Program closes after user input, no reason it should

sorry, I'm very new and have to hurry, so I just wanted to put this up. The code randomly ends after trying the first input (choosing what topic, algebra, basic mathematics, etc.) I've tried other sources, tried reformatting and still don't know what it is. I'm fairly new, so I assume it's just a stupid issue that I overlooked.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
int calculatorChoice, choiceMath1;
int addition1, addition2, additionSum;
int subtraction1, subtraction2, subtractionDifference;
int multiplication1, multiplication2, multiplicationProduct;
int division1, division2, divisionQuotient;
cout << "What would you like to figure out?" << endl;
cout << "" << endl;
cout << "[1]: Basic Mathematic Equations" << endl;
cout << "[2]: Figuring out a Grade" << endl;
cout << "[3]: Algebra" << endl;
cout << "[4]: Inquiry Sciences" << endl;
cout << "[5]: Unit Conversion" << endl;
cin >> calculatorChoice;
if (calculatorChoice == 1) {
cout << "What would you like to do?" << endl;
cout << "" << endl;
cout << "[1]: Addition" << endl;
cout << "[2]: Subtraction" << endl;
cout << "[3]: Multiplication" << endl;
cout << "[4]: Division" << endl;
cin >> choiceMath1;
if (choiceMath1 == 1) {
cout << "Choose your first number." << endl;
cin >> addition1;
cout << "Choose your second number." << endl;
cin >> addition2;
additionSum = addition1 + addition2;
cout << "The sum of " << addition1 << " and " << addition2 << " is " << additionSum << "." << endl;
}
else if (choiceMath1 == 2) {
cout << "Choose the first number." << endl;
cin >> subtraction1;
cout << "Choose the second number." << endl;
cin >> subtraction2;
subtractionDifference = subtraction1 - subtraction2;
cout << "The difference of " << subtraction1 << " and " << subtraction2 << " is " << subtractionDifference << "." << endl;
}
else if(choiceMath1 == 3) {
cout << "Choose the first number." << endl;
cin >> multiplication1;
cout << "Choose the second number." << endl;
cin >> multiplication2;
multiplicationProduct = multiplication1 * multiplication2;
cout << "The product of " << multiplication1 << " and " << multiplication2 << " is " << multiplicationProduct << "." << endl;
}
else if(choiceMath1 == 4) {
cout << "Choose the first number." << endl;
cin >> division1;
cout << "Choose the second number." << endl;
cin >> division2;
divisionQuotient = division1 / division2;
cout << "The quotient of " << division1 << " by " << division2 << " is " << divisionQuotient << "." << endl;
}
else {
cout << "That is not a choice." << endl;
}
}
else {
}
}
void calculator() {
}
Your code doesnt have any loop, what means, after enter the input , it shows the result and program finishes. If you want to stop your code somewhere (depends on the IDE, this solution is for VS) just type system("pause")and you will see what your program shows on console. In case you are not on VS, add #include <cstdlib>
If you explain a bit more hat you expect your code to do we can help you easier.

Calc many value with many arithmetic operators c++ [closed]

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 8 years ago.
Improve this question
#ifndef CALC_H
#define CALC_H
class calc
{
double numb1;
double numb2;
public:
calc();
void FUN_SUM();
void FUN_Subtraction();
void FUN_Multiplication();
void FUN_Division();
void FUN_Power();
void FUN_Squareroot();
void FUN_Switch();
void FUN_Loob();
};
#endif // CALC_H
#include "calc.h"
#include <iostream>
#include <cmath>
using namespace std;
calc::calc()
{
numb1 = 0;
numb2 = 0;
}
void calc::FUN_SUM()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Sum;
Result_Of_Sum = numb1+numb2;
cout << "The result of Sum = " << Result_Of_Sum << endl;
}
void calc::FUN_Subtraction()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Subtraction;
Result_Of_Subtraction = numb1 - numb2;
cout << "The result of Subtraction = " << Result_Of_Subtraction << endl;
}
void calc::FUN_Multiplication()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Multiplication;
Result_Of_Multiplication = numb1*numb2;
cout << "The result of Multiplication = " << Result_Of_Multiplication << endl;
}
void calc::FUN_Division()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Division ;
Result_Of_Division = numb1/numb2;
cout << "The result of Division = " << Result_Of_Division << endl;
}
void calc::FUN_Power()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Power;
Result_Of_Power = pow(numb1,numb2);
cout << "The result of Power = " << Result_Of_Power << endl;
}
void calc::FUN_Squareroot()
{
cout << "Enter the tow number you want Square root \n";
cin >> numb1;
double Result_Of_Square_root;
Result_Of_Square_root = sqrt(numb1);
cout << "The result of Square root = " << Result_Of_Square_root << endl;
}
void calc::FUN_Switch()
{
int S;
cout << "Enter the number you operator do you want do it " << endl;
cout << "1- Addition" << endl;
cout << "2- Subtraction" << endl;
cout << "3- Multiplication" << endl;
cout << "4- Division" << endl;
cout << "5- Power" << endl;
cout << "6- Square Root" << endl;
cout << "7- Exit" << endl;
cin >> S;
switch (S)
{
case 1:
FUN_SUM();
break;
case 2:
FUN_Subtraction();
break;
case 3:
FUN_Multiplication();
break;
case 4:
FUN_Division();
break;
case 5:
FUN_Power();
break;
case 6:
FUN_Squareroot();
break;
default :
break;
}
}
void calc::FUN_Loob()
{
char L;
do
{
FUN_Switch();
cout << "Do you want do another operator ( 'y' or 'n'?) \n";
cin >> L;
if (L== 'y' || L=='Y' || L=='n' || L=='N')
continue;
else
cout << "you are enter roang later\n";
}
while (L == 'Y' || L == 'y' );
}
#include <iostream>
#include "calc.h"
using namespace std;
int main()
{
cout << "Welcome to my simple Calculator\n";
calc simple_calc;
simple_calc.FUN_Loob();
cout << "\n Tank you for use my App :) :) " << endl;
return 0;
}
my question is how i can enable user Calc any value with operators like regular Calculator
for example 1+9*8-1+5 i want my program do like that but i don`t know how ?? :( :(
Writing a calculator is not an easy task, as there is more involved:
Operator Precedence
Grouping
Evaluating of expressions
Operator Precedence
Operator precedence is the grouping of operations, such as performing all multiplication and division before addition and subtraction. A calculator program should handle precedence without parenthesis (grouping).
Grouping
A more advanced calculator will allow grouping of expressions, using parenthesis. The calculator should be able to evaluate the inner most expression and work outward.
Evaluating of Expressions
This means allowing more than 1 digit numbers, and whitespace between numbers and symbols. Also, once the expression is parsed, it needs to be evaluated. The calculator should call the appropriate functions based on the operation (specified by the User).
I suggest you allow the User to enter an expression, and your program read it in as a string. Your program will then parse the string, then evaluate the expression(s).
Search the web and StackOverflow for "c++ calculator" for examples.

How can I avoid bad input from a user?

I am a very newbie programmer, so I don't really know much about writing code to protect the application.. Basically, I created a basicMath.h file and created a do while loop to make a very basic console calculator (only two floats are passed through the functions). I use a series of if and else if statements to determine what the users wants to do. (1.add, 2.subtract, 3.multiply, 4.divide) I used a else { cout << "invalid input" << endl;} to protect against any other values, but then I tried to actually write a letter, and the program entered a infinite loop. Is there anyway to protect against users who accidentally hit a character instead of a number?
`#include <iostream>
#include "basicMath.h"
using namespace std;
char tryAgain = 'y';
float numOne = 0, numTwo = 0;
int options = 0;
int main()
{
cout << "welcome to my calculator program." << endl;
cout << "This will be a basic calculator." << endl;
do{
cout << "What would you like to do?" << endl;
cout << "1. Addition." << endl;
cout << "2. Subtraction." << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division." << endl;
cin >> options;
if (options == 1){
cout << "Enter your first number." << endl;
cin >> numOne;
cout << "Enter your second number." << endl;
cin >> numTwo;
cout << numOne << " + " << numTwo << " = " << add(numOne, numTwo) << endl;
}
else if (options == 2){
cout << "Enter your first number." << endl;
cin >> numOne;
cout << "Enter your second number." << endl;
cin >> numTwo;
cout << numOne << " - " << numTwo << " = " << subtract(numOne, numTwo) << endl;
}
else if (options == 3){
cout << "Enter your first number." << endl;
cin >> numOne;
cout << "Enter your second number." << endl;
cin >> numTwo;
cout << numOne << " * " << numTwo << " = " << multiply(numOne, numTwo) << endl;
}
else if (options == 4){
cout << "Enter your first number." << endl;
cin >> numOne;
cout << "Enter your second number." << endl;
cin >> numTwo;
cout << numOne << " / " << numTwo << " = " << divide(numOne, numTwo) << endl;
}
else {
cout << "Error, invalid option input." << endl;
}
cout << "Would you like to use this calculator again? (y/n)" << endl;
cin >> tryAgain;
}while (tryAgain == 'y');
cout << "Thank you for using my basic calculator!" << endl;
return 0;
}
`
One way would be to use exception handling, but as a newbie you're probably far from learning that.
Instead use the cin.fail() which returns 1 after a bad or unexpected input. Note that you need to clear the "bad" status using cin.clear().
A simple way would be to implement a function:
int GetNumber ()
{
int n;
cin >> n;
while (cin.fail())
{
cin.clear();
cin.ignore();
cout << "Not a valid number. Please reenter: ";
cin >> n;
}
return n;
}
Now in your main function wherever you are taking input, just call GetNumber and store the returned value in your variable. For example, instead of cin >> numOne;, do numOne = GetNumber();
When you input to cin, it is expecting a specific type, such as an integer. If it receives something that it does not expect, such as a letter, it sets a bad flag.
You can usually catch that by looking for fail, and if you find it, flush your input as well as the bad bit (using clear), and try again.
Read a whole line of text first, then convert the line of text to a number and handle any errors in the string-to-number conversion.
Reading a whole line of text from std::cin is done with the std::getline function (not to be confused with the stream's member function):
std::string line;
std::getline(std::cin, line);
if (!std::cin) {
// some catastrophic failure
}
String-to-number conversion is done with std::istringstream (pre-C++11) or with std::stoi (C++11). Here is the pre-C++11 version:
std::istringstream is(line);
int number = 0;
is >> number;
if (!is) {
// line is not a number, e.g. "abc" or "abc123", or the number is too big
// to fit in an int, e.g. "11111111111111111111111111111111111"
} else if (!is.eof()) {
// line is a number, but ends with a non-number, e.g. "123abc",
// whether that's an error depends on your requirements
} else {
// number is OK
}
And here the C++11 version:
try {
std::cout << std::stoi(line) << "\n";
} catch (std::exception const &exc) {
// line is not a number, e.g. "abc" or "abc123", or the number is too big
// to fit in an int, e.g. "11111111111111111111111111111111111"
std::cout << exc.what() << "\n";
}

Quotient program help me [closed]

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 8 years ago.
Improve this question
Hi i have problem with c++
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
float a,b,wynik;
cout << "quotient" << endl
<< "..." << endl << endl
<< "quotient 2 numbers."
<< "\ndivisior does not equal 0"<< endl << endl;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
if (b!=0)
cout << "\n" << a << " / " << b << " = " << a/b << "\n\n";
else
cout <<"\ndivisior does not equal 0!\n\n";
system("PAUSE");
return 0;
}
I must use for or while when someone try quotient number by 0.
If I understood correctly, you probably want to ask for input again if b is zero
You can have something like this :
do {
cout << "quotient" << endl
<< "..." << endl << endl
<< "quotient 2 numbers."
<< "\ndivisior does not equal 0"<< endl << endl;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
if (b!=0)
cout << "\n" << a << " / " << b << " = " << a/b << "\n\n";
else
cout <<"\ndivisior does not equal 0!\n\n";
}while(b==0);
Your terms are wrong, see Wikipedia definition of "quotient".
#include <iostream>
int main(void)
{
char prompt[] =
"\n"
"Division, dividend / divisor = quotient:\n"
" Enter divisior: ";
int divisior;
int dividend;
while (true)
{
std::cout << prompt;
std::cin >> divisor;
if (divisor == 0)
{
std::cout << "\n* Divisor is zero, try again.\n";
continue;
}
std::cout << "\n Enter dividend: ";
std::cin >> dividend;
std::cout << "\nResult of "
<< dividend
<< " / "
<< divisor
<< " is, using integer division, "
<< dividend / divisor
<< "\n";
break;
}
return EXIT_SUCCESS;
}