0

I am new to c++ so I am not sure what I am doing or what's wrong any help ? I am just trying to do a simple equation of motion question where I find the initial velocity of a ball that reaches a max height of 200 meters in 30 seconds. the error in the title is what I keep getting

#include <iostream>
#include <cmath>

 struct Ball {
     float yf;
     float v;
 };

 using namespace std;
 void velocity (Ball b)
 {
     cout<<"words"<<(b.v = (b.yf - 0.5*9.8*900)/30)<<endl; 
     return;
 }

 int main()
 {
     velocity(Ball b);
     return 0 ;
 }
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
  • Also the reason I wrote it this way was because I have to use structure files and then create function files and then connect them through include and run them – Andrew Barajas Feb 09 '21 at 05:33
  • 1
    What do you think that `Ball b` used as an argument of `velocity` call in `main` should do? – Daniel Langr Feb 09 '21 at 05:35
  • I assumed it just got the structure features of velocity and position of the ball and insert them in the function ? I was following an example my professor did – Andrew Barajas Feb 09 '21 at 05:55
  • 1
    @AndrewBarajas Looks like you need to revise a few basic concepts, e.g. functions, variables, types, expressions etc. First chapter of your [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Don't get left behind at this early stage, because it is going to get harder. – john Feb 09 '21 at 06:40

2 Answers2

1

The program won't do what you're actually expecting from it. You want the code to call the method that you have defined above the main function. In fact, you don't call it because of a syntactic error.

You need to create a structure instance and utilize it in the method:

Ball b{0.5, 0.3}; // declaring and initializing with some random nums.
velocity(b);      // using 'b' (type of Ball)
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • Ball b{0.5,0.3} is giving the values to the structure of the ball ? – Andrew Barajas Feb 09 '21 at 06:06
  • @AndrewBarajas yes, this is also a valid initialization. – Rohan Bari Feb 09 '21 at 06:07
  • what is velocity (b); doing here ? sorry I know very little – Andrew Barajas Feb 09 '21 at 06:10
  • @AndrewBarajas `b` is passed to `velocity()` function. – Rohan Bari Feb 09 '21 at 06:16
  • struct Ball { float yf; }; Ball b{200.0}; //using namespace std; void velocity(b) { cout << "words" << (velocity = (b.yf - 0.5 * 9.8 * 900) / 30) << endl; return; } int main () { velocity (b); return 0; } – Andrew Barajas Feb 09 '21 at 06:19
  • main.cpp:19:16: error: variable or field ‘velocity’ declared void void velocity(b) ^ main.cpp: In function ‘int main()’: main.cpp:27:14: error: ‘velocity’ was not declared in this scope velocity (b); ^ – Andrew Barajas Feb 09 '21 at 06:20
  • @AndrewBarajas `velocity` is NOT a variable. And you cannot use a function name as a variable. – Rohan Bari Feb 09 '21 at 06:24
  • 1
    ohh right do you have any other suggestions or sources for my own personal studying ? – Andrew Barajas Feb 09 '21 at 06:41
  • @AndrewBarajas Read the K&R ANSI C programming book to make the fundamentals strong and keep practicing! – Rohan Bari Feb 09 '21 at 06:44
  • 2
    @AndrewBarajas Read any [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for beginners. Don't start with C until you explicitly want/need to. C and C++ are two different languages. C++ is not just C with classes as many programmers use it. – Daniel Langr Feb 09 '21 at 07:01
-1

You did not declare an object of the structure you created. and used it