So i wanted to build a program in which scientific sums are solved along with the steps .I tried but i failed .
Here is the code ~

#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <conio.h>


using namespace std;
int main(){
    int reply;
int u ,v ,t;
int a = v-u/t;
cout << "1.Press 1 to find out acceleration" << endl;
cout << "2.Press 2 to find out velocity time relation"<< endl;
cout << "3.Press 3 to find out postive time relation" << endl;
cout << "4.Press 4 to find out positive velocity relation" << endl;
cin >> reply;

if (reply==1){

cout <<"enter your initial velocity(u)" << endl;
cin >> u;
cout << "enter your final velocity(v)" << endl;
cin >> v;
cout << "enter your time(t)" << endl;
cin >> t;
cout << "The formula is - a=v-u/t " << endl;
cout << "Therefore " << "a=" << v << "-" << u << "/" << t << endl;
cout << "=" << "a=" << v-u << "/" << t << endl;
cout <<"=" << a << endl;
}

system("pause");
}

Its incomplete ,but even the first code to find out the acceleration is not working .

For eg ~ When i enter the intial velocity as 20 and final velocity as 20 and time as 10 it shows up correctly up to " 0/10 "
But when printing the final result that is "v-u/t" it fails terribly .
A long number like "20001002" something comes up .
I am still a beginner but would like to know how to fix this issue .
Help would be greatly appreciated .

Dani AI

Generated

Two separate issues explain the strange output.

First: the program in the first post computes a before any input. That assigns a from uninitialized variables, which is undefined behavior and produces “garbage” numbers (the big 20001002 you saw). Second: after moving the math into a function, the result 18 came from operator precedence and integer division — z - x / y is evaluated as z - (x / y), and with integers 20 - 20/10 becomes 20 - 2 = 18. Parentheses and the right numeric type fix both problems.

Fix checklist (apply in this order):

  • Declare numeric variables after deciding whether you want fractional results; use double (or float) for non-integer acceleration.
  • Read inputs first, then compute. Never initialize a computed variable with other variables that haven’t been set.
  • Use parentheses around the numerator: (v - u) / t.
  • Check t for zero (avoid division-by-zero).
  • When printing steps, show the formula, the substituted expression with parentheses, then the final computed value. Use std::fixed/std::setprecision for readable decimals.

Example (minimal, different from earlier snippets):

#include <iostream>
#include <iomanip>

int main() {
    double u, v, t;
    std::cout << "u v t: ";
    if (!(std::cin >> u >> v >> t)) return 0;
    if (t == 0) { std::cerr << "time must not be zero\n"; return 1; }
    double a = (v - u) / t;
    std::cout << "a = (v - u) / t\n"
              << "a = (" << v << " - " << u << ") / " << t << " = "
              << std::fixed << std::setprecision(3) << a << "\n";
    return 0;
}

Notes: avoid system("pause") and Windows-only headers for portability; when using helper functions, name parameters clearly and return (v - u) / t (with parentheses). This is what pointed out, and it matches @im_abcd’s final working result once corrected.

Recommended Answers

All 4 Replies

Your formula needs to be computed inside a function so at line 11 you have a big error as you are trying to initialize an integer variable with the result of an operation of three other uninitialized variables.

//ex
float density(float mass,float volume)  {
   return mass/volume;
 }

Your formula needs to be computed inside a function so at line 11 you have a big error as you are trying to initialize an integer variable with the result of an operation of three other uninitialized variables.

//ex
float density(float mass,float volume)  {
   return mass/volume;
 }

Umm,could you show what editing should be done in my program ?

I added

int printAcceleration(int z , int x , int y){
return z-x/y;
}

outside int main()

and

cout <<"=" << printAcceleration(u,v,t) << endl;

instead of " a "

But still when i try to put the two velocity's as 20 ,20 and time as 10 the ans is 18 .
i would really appreciate if you could edit my program and point out my mistake .

commented: Of course. You didn't get it the first time so you need us to finish your homework. Really lame!!! -4

You need take into account the operator precedence rules.I believe you meant (z-x)/y.

You need take into account the operator precedence rules.I believe you meant (z-x)/y.

Excellent answer ,now its working perfectly ,Thank you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.