Hi, can someone give me an easy command that prevents a user from entering a negative value in the following code i made? thanks.

#include <iostream>

using namespace std;

int main()
{
   int X;
   int Y;
   cout<<"please enter the value of X and Y";
   cin>>X;
   cin>>Y;
   float result;
   result=4*X^3+Y^3-4;
   cout<<"the result of Z="<<result;
    return 0;
}

Dani AI

Generated

Good suggestions from and about looping on input. Two additional practical points that the thread hasn’t shown yet: the original expression 4*X^3+Y^3-4 is incorrect because ^ is bitwise XOR in C++ (not exponentiation), and simple cin >> loops can still break if the user types non-numeric text. A safer pattern is to read a full line, parse it, and reject it unless it parses exactly to a non-negative integer.

Use a small helper that reads a line, parses with std::istringstream, and ensures there are no extra characters. That prevents crashes on strings and rejects things like "12abc". Compute cubes with integer multiplication (or carefully with std::pow if you need floating-point), and watch for overflow if inputs can be large.

#include <iostream>
#include <string>
#include <sstream>

long long readNonNegative(const std::string &prompt) {
    std::string line;
    while (true) {
        std::cout << prompt;
        if (!std::getline(std::cin, line)) std::exit(1);
        std::istringstream iss(line);
        long long v;
        char extra;
        if (iss >> v && !(iss >> extra) && v >= 0) return v;
        std::cout << "Please enter a non-negative integer.\n";
    }
}

int main() {
    long long x = readNonNegative("Enter X: ");
    long long y = readNonNegative("Enter Y: ");
    long long result = 4 * x * x * x + y * y * y - 4; // correct cube calculation
    std::cout << "Z = " << result << '\n';
}

Notes: avoid reading into unsigned types for user input (they convert negatives strangely). pow returns floating-point and may introduce rounding; prefer integer multiplication for exact cubes. Compile with warnings enabled (e.g., -Wall) to catch surprises.

Recommended Answers

All 2 Replies

Sure. Initialize X and Y to say -1. Now put a while test for X and Y being negative around lines 9 to 11.

commented: im sorry what does that mean +0

Is there a specific reason you need to ensure that the values are positive?

As per RProffitt's suggestion, here is how you would loop on the input to require that the input value is positive.

#include <iostream>

using namespace std;

int main()
{
   int X = -1;
   int Y = -1;
   while (X < 0)
   {
       cout<<"please enter a positive value for X";
       cin>>X;
   }
   while (Y < 0)
   {
       cout<<"please enter a positive value for Y";
       cin>>Y;
   }
   float result;
   result=4*X^3+Y^3-4;
   cout<<"the result of Z="<<result;
   return 0;
}

A simpler alternative is to use abs() (which requires you to #include <cmath>) to force the value positive regardless of whether it was negative or not. However, looping on the input is advisable in any case, to hand the instance where the input is a string.

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.