Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
When I tried to give the string input like "Hello" it is producing an error. How can I check that when I give string input string it should ask me to give correct input?
int y,m,d,h,min,s;
do
{
cout<<" Please enter the year: ";
cin>>y;
}while(y < 1970 || y > 2020);
#include "iostream"
#include<limits>
using namespace std;
int input()
{
int y;
do
{
std::cin.clear();
std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
std::cout << "Give the year" << std::endl;
std::cin >> y;
}
while (std::cin.fail() || y < 1970 || y > 2020);
return y;
}
main()
{
int x = input();
}
If the input cannot be converted to a int (in your case), then the failbit will be set for std::cin. This can be retrieved by calling cin.fail().
std::cin >> y;
if (std::cin.fail()) {
std::cout << "data entered is not of int type";
}
You can also use !std::cin instead of std::cin.fail().
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have faced the problem the formula of percentage doesn't work properly
#include <iostream>
using namespace std;
int main() {
int tmarks,intermarks, passmarks;
float per;
cout << "Enter Your Inter Marks:\n";
cin >> intermarks;
cout << "Enter Your Total Marks:\n";
cin >> tmarks;
cout << "Enter Your PassMarks:\n";
cin >> passmarks;
per = (intermarks/tmarks) * 100;
cout << "percentage:" << per;
if (per >= 45 && passmarks >= 50) {
cout << "Welcome To Uni\n";
} else {
cout << "Improve Your Marks You are eligible\n";
}
}
If intermarks = 50 and tmarks = 75, then intermarks/tmarks will be 0. Since both are integers. You need to typecast before division operation. This way float(intermarks) / float(tmarks) will be 0.67 and per will be 67
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
It's really complicated to me as a beginner to do it so I tried this code :
int x,sum=0;
while (x)
cin >> x;
sum+=x;
cout << sum ;
I want to let the program when the user enters "0" the program should print the sum of these numbers entered by the user.
One way to do it is here:
#include <iostream>
int main()
{
int x;
int sum = 0;
std::cin >> x;
while(x)
{
sum += x;
std::cin >> x;
}
std::cout << "Sum: " << sum << std::endl;
return 0;
}
You are missing {}s around the statements in your while loop, so only cin >> x gets executed.
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 6 years ago.
Improve this question
Here is my first program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a;
string s;
double d;
while(cin >> a >> s >> d)
cout << a << s << d;
return 0;
}
When I input some simple data and press Enter , the result is shown immediately:
However, the code in another program behaves differently:
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double price = 0.0;
void Print();
};
void Sales_data::Print(){//print every record of the Sales_data
cout << "The bookNo of the book is " << bookNo << endl;
cout << "The units_sold of the book is " << units_sold << endl;
cout << "The price of the book is " << price << endl;
}
int main()
{
Sales_data book;
while(cin >> book.bookNo >> book.units_sold >> book.price);
book.Print();
return 0;
}
When I run this code, input some data, and press Enter, it waits for me to input more data rather than show the result.
Could you explain this to me?
Remove the semicolon after the while loop. As it is, it forces the loop to have no body, which means it just cycles over the cin forever. Even better, use braces to delimit the body:
while(cin >> book.bookNo >> book.units_sold >> book.price) {
book.Print();
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
No idea where the issue is coming from so I will insert the whole sub-routine.
When you insert a string into the guess space it will loop infinitly, if you insert a number it will return "ion do you want to go?"(which isn't even written anywhere in the program).
void guess(){
int guess;
string guess2;
string guess_status="";
bool win;
int attempts;
int counter;
int num;
while (guess2 != "exit"){
num=rand() %100 + 1;
win=0;
counter=0;
while (win != 1){
attempts=5-counter;
cout << "Guess a number Attempts Left: " << attempts << endl;
cout << "between 1 and 100 ============================\n The Guesing Game\n ============================" << endl;
cout << "\n" << guess_status << endl;
cout << "\nGuess: ";
cin >> guess;
system("cls");
if (!cin) {
guess2=guess;
if (guess2 != "exit"){
guess_status="Please insert a valid number, restarted game.";
win=1;
}
}
if (cin){
if (guess==num){
win=1;
guess_status="Correct! Generated new number.";
}
if (guess != num){
if (guess < num){
guess_status=num +"was too low!";
}
if (guess > num){
guess_status=num +"was too high!";
}
}
}
}
}
}
The routine is indented, it just didn't paste that way
int guess;
string guess2;
guess2=guess;
This is your problem. You can't convert an int into a string this way. What you're actually doing is telling the computer that guess2 is a string that starts at the memory address that the value of guess is currently. That's why you're getting a string output that's not even in your program- it's just what happens to be at that address.
See here for how to convert an int to a string:
Easiest way to convert int to string in C++
Also, don't use cin >> guess. Get the input as a string, then check to see if it can be converted to an integer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm new to C++ and I can't work out why the code stops as soon as the user inputs either RecPrisim, TriPrisim or Cylinder the program stops, prints out some random numbers and closes. I'm just wondering if its because the variables need to be numbers but I tried do the same thing using Strings and I got errors.
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
int Length;
int Height;
int Base;
int Width;
int UserChoice;
int ObjectResult;
int RecPrisim;
int TriPrisim;
int Cylinder;
int TriResulta;
RecPrisim = 1;
TriPrisim = 1;
Cylinder = 1;
TriResulta = 1;
cout << "Choose one: RecPrisim, TriPrisim or Cylinder." << endl;
cin >> UserChoice;
if (UserChoice = RecPrisim)
{
cout << "Enter Length, Width then Height.";
cin >> Length;
cin >> Width;
cin >> Height;
ObjectResult = Length*Width*Height;
cout << ObjectResult;
}
else if (UserChoice = TriPrisim)
{
cout << "Enter Base, Height, Width, Length." << endl;
cin >> Base;
cin >> Height;
cin >> Width;
cin >> Length;
ObjectResult = Base*Height / 2 * Width*Length;
cout << ObjectResult;
}
else if (UserChoice = Cylinder)
{
cout << "Enter Radius and Length." << endl;
cin >> Base;
cin >> Height;
ObjectResult = 3.1459*Base*Base*Height;
cout << ObjectResult;
}
system("pause");
}
Use == instead of =.
In C++, C and many more languages == is for comparing values whereas = is for assigning values.
If you want to initialise a variable say test with value val, then you should use test = val.
But in if conditions you (generally) want to compare values using a comparison operator like following
== for comparing whether LHS is equal to RHS
> for comparing whether LHS is greater than RHS
< for comparing whether LHS is smaller than RHS
Based on the values the operator will either return true or false and the if condition will either be executed or not.
Since, it seems in your case you want to compare the value of UserChoice with some other value for equality, you should use == instead of =.
Please use ==, not the =.For example,
the code if(a = 1) will be always true no matter what value of a, because if(1) is always true. Only the code if(a == 1) is what you want. I hope this can help you.