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];
Related
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.
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
My program won't run I get an error message saying:
`error: no match for 'operator<<' (operand types are 'std::string' {aka 'std::__cxx11::basic_string<char>'} and '<unresolved overloaded function type>')|`
On the cout << address, " ", street << endl; line
I was using VS2017 but switched to CodeBlocks mid-way through
I have Windows 10 Pro Ryzen 5 2400G, 1060 6gb 16gb ram
Here is my program:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, city, state, road, country, street;
int address;
cout << "Enter your: Name\n";
cin >> name;
cout << "Enter your Street\n";
cin >> street;
cout << "Enter your: Address\n";
cin >> address;
cout << "Enter your:\n City\n";
cin >> city;
cout << "Enter your: Province/State\n";
cin >> state;
cout << "Enter your: Country\n";
cin >> country;
//Output
cout << name << endl;
cout << address, " ", street << endl;
cout << city, " ", province, " ", country;
}
Thanks in advance!
Your syntax is wrong. You can't use , to chain arguments to cout like that. Instead do:
cout << address << " " << street << endl;
cout << city << " " << province << " " << country;
Your last two statements are syntactically incorrect. They should be as follows -
cout << address<<" "<<street << endl;
cout << city<< " "<< province<<" "<< country;
You probably are trying to use something like python in C++. But obviously that doesn't work. Each time you just have to keep doing cout<< variable1 << " " << variable2<< " "; . This is how chaining works in C++. No shorthand to that
I hope this solves your issue!
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
There is a program I'm building in which I need to calculate torque and diameter. I have both equations in syntax but my output keeps being zero. What am I missing / doing wrong? (Disclaimer: I'm very new to this so please, I welcome constructive criticism! Anything to help me get better. Thanks).
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double p, n, s, t, d;
int main()
{
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
cin >> p, n, s;
t = 6300 * p / n;
d = pow((16 * t) / s, 0.333);
cout << setw(10) << "HP " << p << endl;
cout << setw(10) << "rpm " << n << endl;
cout << setw(10) << "psi " << s << endl;
cout << setw(10) << "torque " << t << endl;
cout << setw(10) << "diameter " << d << endl;
return 0;
}
//Output:
/*Enter values for horsepower (p), rpm (n) and shear strength (s): 20 1500 5000
HP 20
rpm 0
psi 0
torque inf
diameter inf */
The intent of the lines
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
cin >> p, n, s;
is not correctly represented in code.
The second line does not read anything into n or s. It's an expression that uses the comma operator. In this particular case, it evaluates n and s and discards the values.
It's as if you have:
cin >> p;
n;
s;
Change that line to:
cin >> p >> n >> s;
You can also be verbose and use:
cin >> p;
cin >> n;
cin >> s;
Take your input like this (cin >> p >> n >> s) way and also print your result after result calculation you are are printing before.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double p, n, s, t, d;
int main()
{
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
// cin >> p, n, s;
cin >> p >> n >> s;
cout << setw(10) << "HP " << p << endl;
cout << setw(10) << "rpm " << n << endl;
cout << setw(10) << "psi " << s << endl;
t = 6300 * p / n;
d = pow((16 * t) / s, 0.333);
cout << setw(10) << "torque " << t << endl;
cout << setw(10) << "diameter " << d << endl;
return 0;
}
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;
}
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 9 years ago.
Improve this question
I have a problem in the for-construction, I don't know why my counting starts from 0 but you can't write on it(you can write starting with Register 1).I've marked in the code where is the problem.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string day, date;
int registerCount;
std::cout << "INPUT DATA"
<< std::endl << std::endl
<< "Enter the day in which you want to perform the register: "
<< std::endl;
std::cin >> day;
std::cout << "DATE:" << std::endl;
std::cin >> date;
std::cout << "Enter the number of registers you wanna perfom for the day "
<< day << ":" << std::endl;
std::cin >> registerCount;
std::vector<std::string> registers(registerCount);
for (int i = 0; i < registerCount; ++i) **here is the problem**
{
std::cout << "Register " << i << ":" << std::endl; **it starts from 0 but you can't write on Register 0**
std::getline(std::cin, registers[i]);
}
std::cout << "The data for the day of " << day << " are the following: "
<< std::endl;
std::cout << "DATE: " << date << std::endl;
for (int i = 0; i < registerCount; ++i)
std::cout << registers[i] << std::endl;
}
Use
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Header - #include<limits>
after your
std::cin >> registerCount;
to "eat up" the trailing newline coming from previous std::cin which is cause of skipping of the first input
for (int i = 0; i < registerCount; ++i)
You're defining i as starting at 0 here. Very simply, all you need to do is change the "i = 0" to "i = 1".
The way you're assigning to that vector looks a little bit wrong too. I think you need to use push_back() on the vector in that syntax, but I'm not too familiar with all the different operators for vector.