Exponents and square roots as user input [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How can I allow the user to enter a number in the form of e^x, or sqrt(x) when asked, instead of just entering a number in numerical form? Thanks.

Generally this is a question of parsing. In the case of these two forms exactly, a simple solution could be something like:
string input;
getline(cin, input);
if (input[0] == 'e' && input[1] == '^') {
int num = atoi(input.substr(2).c_str()); // probably better to use stringstreams here
cout << exp(num) << endl;
} else if (input.substr(0, 5) == "sqrt(" && input[input.size() - 1] == ')') {
int num = atoi(input.substr(5, input.size() - 6).c_str());
cout << sqrt(num) << endl;
} else {
cout << "error" << endl;
}
Didn't test that but should be roughly right. If you need to handle more nuanced cases or more than just these 2 cases, you'll have to do some more elaborate parsing.

Related

Why do i have to reference in this situation? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
int main(){
while(true){
char input = getchar();
int x, y;
POINT xypos;
if (input == 'S' || input == 's'){
std::cout <<"enter new position" << std::endl;
std::cin >> x >> y;
SetCursorPos(x, y);
} else if (input == 'g' || input == 'G'){
GetCursorPos(&xypos);
std::cout << "X: " << xypos.x << "Y " << xypos.y << std::endl;
}
}
return 0;
}
Can someone please explain why with GetCursorPos, it has to reference the xypos object in the parameters? Why is it not possible to directly utilize it? Thanks
You probably mean why GetCursorPos(from the WinAPI) doesn't just return the position instead of taking a pointer and filling that right?
That's how the WinAPI works, almost all functions return BOOL to indicate success or failure and take information they populate by pointer.

Getting same output everytime [closed]

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 a simple number guessing game. If you guessed the number right, it outputs "You win!!!", but if the number of tries (numberofguesses) is exceeded, it should output "You lose", but it is showing "You win!!!" even though I checked the values of numberofguesses, secretnum and guess after the while loop. Answer in simple words, I'm a beginner.
#include <iostream>
using namespace std;
int main()
{
int secretnum = 7;
int guess = 0;
int numberofguesses = 3;
while (secretnum != guess && numberofguesses != 0) {
cout << "enter your guess: ";
cin >> guess;
--numberofguesses;
}
if (secretnum = guess && numberofguesses != 0) {
cout << "You win!!!";
}
else
{
cout << "You lose";
}
}
You have mistaken the assignment operator = with the comparison operator ==.
In this line here:
if (secretnum = guess && numberofguesses != 0)
cout << "You win!!!";
Change it to:
if (secretnum == guess && numberofguesses != 0) {
cout << "You win!!!";

How I can stop the " while loop " if the user enter number <0? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
in this example :
int numbers=0 ,sum=0;
while (numbers >=0)
{
cout<<"Enter positive numbers: ";
cin>>numbers;
sum += numbers;
}
cout<<"The result = "<<sum<<"\n";
Can u help me what I should to do please?
In the loop, you'll have to deal with two situations.
The user enters invalid input or EOF.
The user enters a number less than 0.
For the first, you'll need to use:
if ( cin >> numbers )
{
// Reading to numbers was successful.
}
else
{
// Deal with the error.
}
For the second situation, you'll need to use:
if ( numbers < 0 )
{
break;
}
Put it all together,
while ( true )
{
cout << "Enter positive numbers: ";
if ( cin >> numbers )
{
if ( numbers < 0 )
{
break;
}
}
else
{
// Deal with error. Perhaps break out of the loop too?
break
}
sum += numbers;
}
cout << "The result = " << sum << "\n";
You could add:
if (numbers < 0) {
return;
}

C++ while (true) vs while (!something) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
So let's say we have a bit of code in which we want the user to say something specific, but we want to give them infinite tries if they get it wrong. I would use a do while loop.
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout<<"Enter yes or no: ";
do {
std::cin>>input;
if (input == "yes") {
break;
} else if (input == "no") {
break;
} else {
std::cout<<std::endl<<"Enter yes or no: ";
}
} while (true);
}
But, I have been told that it is bad practice to use while (true);, and, (in this case), I should instead use while (input != "yes" && input != "no");. Which one of them is correct?
std::cout<<"Thanks!";
The loop should test for input-failure (you forgot), and then whether you have to loop on. Thus, it should be:
while ((std::cin >> input) && input != "yes" && input != "no")
std::cout << "\nEnter yes or no: ";
You shouldn't use std::endl when you don't need an explicit flush.
Or, better yet, encapsulate it in a re-usable function, and throw an exception on input-failure.

Trouble testing for prime numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi I'm doing a prime number work for class, and I'm running into a problem. Could someone please help me a little bit?
With some numbers it will work but other numbers it won't:
#include<iostream>
#include "cmath"
#include "ctime"
using namespace std;
int main(){
int num;
int i = 2;
int result;
cout << "What is the prime you want to enter";
cin >> num;
for(; i < num; i++){
result = num / i;
}
if (num % i == 0) {
cout << "Your number is not prime\nIt is divisble by: " << i << endl;
}
if(num % i != 0){
cout << "your number is prime\n";
}
return 0;
}
this if (num % i == 0) { .... must be done inside the loop, otherwise you are going to check only the last value of i