Solve C++ Program Issue: Compute Square Root & Compare Difference >50

  • Context: C/C++ 
  • Thread starter Thread starter Lykin
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming problem involving the computation of square roots and comparing their differences. Participants are addressing issues related to syntax errors, logic implementation, and the use of while loops in the context of this programming task.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to solve a C++ problem but encounters issues with while loops and syntax errors in their code.
  • Another participant points out specific errors, such as an extra closing brace, the misuse of comparison operators, and the need for returning values from functions.
  • A participant acknowledges the corrections and expresses a desire to understand while loops better, indicating they plan to try a variation of the suggested code.
  • There is a mention of a potential misunderstanding regarding variable assignment versus comparison in the code.
  • A later reply humorously questions the original poster's experience with programming languages, suggesting a possible background in Fortran.

Areas of Agreement / Disagreement

Participants generally agree on the identification of syntax errors and the need for clearer logic in the code. However, there is no consensus on the best approach to implement the while loop or the overall structure of the program.

Contextual Notes

Some limitations include unresolved aspects of how to effectively implement the while loop and the overall program logic, as well as the potential for confusion regarding variable passing and function calls.

Lykin
Messages
4
Reaction score
0
I'm attempting to teach myself C++, and I came across this old C++ problem in my closet. It's a ripped page and I'm not sure what book it's from so it has no solution. It's easy, but I seem to have issues with while loops. If someone would explain the logic or show me an example as to how they would do it that would be much appreciated.

The problem:
-Construct a program that computes the square root of a number. Make compare the difference from your first input to your second input, and so on and so forth (for example, if the first number you input is 4, compare the square root of that to the square root of the next number you input). If the difference of the two numbers is greater than 50, terminate the program.

I have something like this:

Code:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>

double positivePrint (double num, double square);

int main ()
{
    double num, square;
    
    positivePrint(double num, double square);
}

double positivePrint (double num, double square)
{
       num==1;
       while(num>0, num++);
       {
                    square=sqrt(num);
                    cout << square << endl;
       }
}
       }
}

But that gives me an "expected primary expression before" error and I'm not really sure where to proceed from here anyhow. Any help is appreciated and thanks in advance.
 
Technology news on Phys.org
you have an extra

Code:
 }
}
at the end. == is a comparison, not an assignment. having a ; after the num++) ends the while loop there. you arent returning a value from positivePrint or main. Also look up passing by reference , cause i think your code is intending to do that.

for Construct a program that computes the square root of a number. Make compare the difference from your first input to your second input, and so on and so forth (for example, if the first number you input is 4, compare the square root of that to the square root of the next number you input). If the difference of the two numbers is greater than 50, terminate the program.

One implementation is:

Code:
#include <iostream>
#include <math.h>

int main(void)
{
double num1=0;
double num2=0;
std::cout << "Enter num1: ";
std::cin >> num1;
std::cout << "Enter num2: ";
std::cin >> num2;
double sqrtnum1=sqrt(num1);
double sqrtnum2=sqrt(num2);
std::cout << "The difference of sqrt of both nums is " << fabs(sqrtnum1-sqrtnum2);
if (fabs(sqrtnum1-sqrtnum2) >50)
{
return 1;
}
return 0;
}

or something like that. Terminates with 1 if the difference is greater than 50, terminates with 0 if it isnt.
 
Last edited:
Thanks for the quick response.

I didn't notice the extra braces and using == instead of = was just dumb, but that fixed my syntax error. I read through your code and understand what you're doing - I think I was trying to do to much by trying to call another function in main. However, I still don't really understand how to implement and use while loops.

Not on my home PC now, but I plan on trying a variation of your code when I get home. Once again I thank you for your quick help.
 
You also appear to have "n== 1" where you surely intended "n= 1".
 
DrKn - why you didn't intend code? Did you have too much experience with fortran 60?
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
7K
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
6K