Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
float distance(tocki *A, tocki *B);
int main()
{
struct tocki{
int x, y;
};
tocki A, B, C;
cout << "x = ";
cin >> A.x;
cout << "y = ";
cin >> A.y;
cout << "x = ";
cin >> B.x;
cout << "y = ";
cin >> B.y;
cout << "x = ";
cin >> C.x;
cout << "y = ";
cin >> C.y;
cout << distance(&A, &B);
return 0;
}
//distance between (x1,y1) i (x2,y2) e d = sqrt((x2-x1)^2 - (y2-y1)^2);
float distance(tocki *A, tocki *B){
return sqrt(pow(A.y - A.x, 2) - pow(B.y - B.x, 2));
}
The errors I'm getting are:
'tocki' was not declared in this scope
'A' was not declared in this scope
'tocki' was not declared in this scope
'B' was not declared in this scope
On this line:
float distance(tocki *A, tocki *B);
So, what exactly am I doing wrong here? I want to pass a struct to function and get the result of the function in my main() program.
place the toki struct outside the main function
The struct tocki should be declared before the declaration of the function distance, so the compiler knows that the struct exists when check the type of the parameters. Moreover, you should use A->y ecc. , because you have passing a pointer to the struct.
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 my first post. I am having a issue getting my CML calculator to run, I know there are better ways to make a calculator than the code I have, so please leave suggestions and such in the comments, as i am a beginner programmer and it is important to receive feedback.
functions practice.cpp
#include "pch.h"
#include <iostream>
#include "functions.cpp"
using namespace std;
int main()
{
int x;
int y;
char oper;
cout << "Welcome to this test calculator" << endl;
cout << "Please Enter a operator to use: " << endl;
cin >> oper;
cout << "Enter a x and y value " << endl;
cin >> x, y;
calculate(x, y, oper);
return 0;
}
functions.cpp
#include "functions.h"
#include "pch.h"
#include <iostream>
using namespace std;
int calculate(int x, int y, char oper) {
switch (oper)
{
case '-':
return x - y;
break;
case '+':
return x + y;
break;
case '*':
return x * y;
break;
case '/':
return x / y;
break;
default:
break;
}
return 1;
}
What am i doing wrong?
In the file practice.cpp:
cin >> x, y; should be cin >> x >> y;
cin >> x, y; will read input only for x.
Here , works as an end of the cin part, and y will work like a separate part.
Understanding of comma operator (,) in the code:
Comma operator will always give priority to the right most operator.
ex: x = (y,z);
In this line, value of z will be assigned to x.
Same way in the line cin >> x, y;, y execute nothing. And after that cin >> x will read input from the user.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
i cant figure out where to put all the loop parts for a simple y/n (repeat/exit) loop.i tried to find answers, but none are clear enough for my particular case.
P.S. iam a beginner at coding, so please dont make it too complicated unless necessary
this is my code so far
#include <stdio.h>
#include <iostream>
using namespace std;
// input function
void Input (float &x, float &y);
float a=1.0, b=1.0, result;
char operation;
char yesNO;
int main ()
{
do {
cout << "Programma wat optelt, aftrekt, vermedigvuldigd en deelt. \n\n";
cout << "Geef een opdracht (eg. 1 + 2): \n";
cin >> a >> operation >> b;
Input (a,b);
cout << "Het antwoord is: " << result << endl;
system ("pause");
return 0;
}
while (yesNO == 'y');
void Input (float &x, float &y)
{
a = x;
b = y;
switch (operation)
{
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout << "foutieve invoer: \n";
cin >> a >> operation >> b;
Input (a, b);
}
}
}
I'll ignore some of the things wrong with your program and answer the question directly.
Two things:
You are never asking the user if they want to continue
You are aborting your loop by returing out of main()
So replace these 2 lines:
system ("pause");
return 0;
with a query that asks the user if they want to continue, and populate the variable yesNO with their answer.
It stops because of the return statement in "int main". I would suggest using "void main ()" instead of "int main ()". But if you want to use "int main ()", shift the "return 0" below the while statement. You also need to ask the user if he or she wants to continue. Try this: (Ignore the bad spacing)
int main () {
do {
cout << "Programma wat optelt, aftrekt, vermedigvuldigd en deelt. \n\n";
cout << "Geef een opdracht (eg. 1 + 2): \n";
cin >> a >> operation >> b;
Input (a,b);
cout << "Het antwoord is: " << result << endl;
cout << "Press y to continue: ";
cin >> yesNo;
} while (yesNO == 'y');
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm new to C++ and I can't work out why the code stops as soon as the user inputs either RecPrisim, TriPrisim or Cylinder the program stops, prints out some random numbers and closes. I'm just wondering if its because the variables need to be numbers but I tried do the same thing using Strings and I got errors.
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
int Length;
int Height;
int Base;
int Width;
int UserChoice;
int ObjectResult;
int RecPrisim;
int TriPrisim;
int Cylinder;
int TriResulta;
RecPrisim = 1;
TriPrisim = 1;
Cylinder = 1;
TriResulta = 1;
cout << "Choose one: RecPrisim, TriPrisim or Cylinder." << endl;
cin >> UserChoice;
if (UserChoice = RecPrisim)
{
cout << "Enter Length, Width then Height.";
cin >> Length;
cin >> Width;
cin >> Height;
ObjectResult = Length*Width*Height;
cout << ObjectResult;
}
else if (UserChoice = TriPrisim)
{
cout << "Enter Base, Height, Width, Length." << endl;
cin >> Base;
cin >> Height;
cin >> Width;
cin >> Length;
ObjectResult = Base*Height / 2 * Width*Length;
cout << ObjectResult;
}
else if (UserChoice = Cylinder)
{
cout << "Enter Radius and Length." << endl;
cin >> Base;
cin >> Height;
ObjectResult = 3.1459*Base*Base*Height;
cout << ObjectResult;
}
system("pause");
}
Use == instead of =.
In C++, C and many more languages == is for comparing values whereas = is for assigning values.
If you want to initialise a variable say test with value val, then you should use test = val.
But in if conditions you (generally) want to compare values using a comparison operator like following
== for comparing whether LHS is equal to RHS
> for comparing whether LHS is greater than RHS
< for comparing whether LHS is smaller than RHS
Based on the values the operator will either return true or false and the if condition will either be executed or not.
Since, it seems in your case you want to compare the value of UserChoice with some other value for equality, you should use == instead of =.
Please use ==, not the =.For example,
the code if(a = 1) will be always true no matter what value of a, because if(1) is always true. Only the code if(a == 1) is what you want. I hope this can help you.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Ok my problem is, in my dynamic array function I have an array that gives me the error below.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
class Revenue
{
};
static int Track_Num_Divisions = 1;
static int Track_Quart_Revenue = 1;
void Program_loop()
{
{
string D;
string DN;
int N;
double TS;
double TC;
double P;
int arry;
cout << "how many Revenue tiers do you want?: "; cin >> arry;
Revenue* rev = new Revenue[arry];//dynamic array
for (int i = 0, Track_Num_Divisions = 1;Track_Num_Divisions, i < arry; i++,Track_Num_Divisions++ )
{
Revenue& rev = rev[i];// THIS IS THE ERROR <<<<
cout << " " << endl;
cout << "Revenue #"<<Track_Num_Divisions << endl;
cout << "===========" << endl;
cout << "<< Ok what is your division name?: " << endl; cin >> D;
string set_Division_name(D);
cout << "<< What is your division number?: " << endl; cin >> DN;
string set_Division_number(DN);
while (DN.size() != 4)
{
cout << "<< Sorry! Your Division Number cannot exceed or be short of 4. " << endl; cin >> DN;
}
delete[] rev;
}
Gives this error in function Dynamic_array
I think the problem lies in this code>> Revenue& rev =rev[i]:
Error 1 error C2676: binary '[' : 'Revenue' does not define this operator or a conversion to a type acceptable to the predefined operator
Error 2 IntelliSense: no operator "[]" matches these operands
operand types are: Revenue [ int ]
What should I do?
I am kinda new to this Website still learning the ropes of proper format.
This line is the problem:
Revenue& rev = *rev[i];
You're dereferencing the value returned by rev[i], but rev[i] is not a pointer or a class with an overloaded operator*. It's a Revenue&.
There's no need for derferencing anything here, just write it as:
Revenue& rev = rev[i];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
#include <iostream>
using namespace std;
void inputArray(double [], int );
void printArray(double [] ,int);
int main()
{
double rainfall[5];
rainfall[0]=1;
rainfall[1]=6;
rainfall[2]=9;
rainfall[3]=23;
rainfall[4]=67;
printArray(rainfall,5);
inputArray(rainfall,5);
}
void printArray(double array[],int size)
{
for(int i=0;i<size;i++){
cout<< "Rainfall is";
cout << array[i] <<endl;
}}
void inputArray(double array[], int size)
{
for(int i=0;i<size;i++){
cout << "Enter the Rainfall:";
cin >> array[i] << endl;
}
}
You can't do this:
cin >> foo << endl;
Near the last line of your code, it looks like you're trying to do something like:
Get some input and put it in array[i]
Echo the input and a new line?
You should do it like:
cin >> array[i];
cout << array[i] << endl;
Remember, cin >> foo means "take some input from the console and put it in foo," and cout << foo means "output foo to the console."
You can't cin "endl", you shoul cout it;
You asking user for entering values, but you don't use it.