my program is
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int exit;
while(1==1)
{
float a,b,c;
printf("Enter length(in metre)");
scanf("%f",&a);
printf("Enter width(in metre)");
scanf("%f",&b);
printf("Enter height(in metre)");
scanf("%f",&c);
if(5.919<a<=12.056 && 2.340<b<=2.347 && 2.380<c<=2.648)
{
printf("40' high cube \n");
}
else if(a<=5.662 && 2.340<b<=2.438 && c<=2.16)
{
printf("20' open top");
}
else
{
printf("40' flat rack");
}
cout << "Would you like to exit?(Yes/No)"<<endl;
cin >> exit;
if(exit == 1)
{
break;
}
}
return 0;
}
But, the code is given wrong asnwers.No matter whatever feed i give..it is selecting 40" high cube
Also,I want to input yes/no to continue,instead of 1. How should I do it?
Unfortunately, while the following is valid syntactically, it does not do what you expect:
5.919<a<=12.056
This is parsed as (5.919 < a) <= 12.056 and works as follows: a is compared to 5.919, and the result of that comparison (0 or 1) is compared to 12.056. Since both 0 and 1 are less than 12.056, the overall expression always evaluates to 1.
To fix, rewrite it as:
5.919 < a && a <= 12.056
The same goes for all the other similar expressions.
For input yes/no, You can use this way..
char exit;
// in while loop waiting for user input
exit = getch();
if(exit == 'y' || exit == 'Y' )
break;
//else again go for input.
This line isn't doing what you think.
if(5.919<a<=12.056 && 2.340<b<=2.347 && 2.380<c<=2.648)
to do what you seem to want, you would need to change it to:
if(5.919<a&& a<=12.056 && 2.340<b&& b<=2.347 && 2.380<c&& c<=2.648)
same for this line:
else if(a<=5.662 && 2.340<b<=2.438 && c<=2.16)
that needs to be changed to:
else if(a<=5.662 && 2.340<b && b <=2.438 && c<=2.16)
When you write this:
2.340 <b <= 2.347
what C and C++ understand is (2.30 < b) <= 2.347 and that is always true (1 in C) for all values of b because (2.30 < b) is either 0 or 1, which is always less or equal than 2.347. This is arcane and backwards, but it is how C and C++ see the world. I'm sure that this will only bite you once, and this was your turn.
Also, it is bad practice to mix printf and scanf with cin and cout, make a choice about how you want to do your I/O and stick to it, mixing them brings other subtle bugs that are difficult to troubleshoot.
For the final part of your question, you can do:
#include<stdio.h>
#include<string.h>
int main(void){
char answer[16];
printf("Yes or No\n");
scanf("%s",answer);
if (strncmp(answer,"Yes",3)==0){
printf("yes\n");
}else{
printf("no\n");
}
}
For the second part question :
Change the condition to Y/N instead of 1 and 0. Use character data type, or string or anything similar. And then you can compare the input with "exit".
Related
In this question what I have to do is: find the no. of digits in the given string which are divisor of the complete number represented by the string.
For numbers without 0 in it it is working fine. for example t=1 , s="12" , output=2
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
long long t,i,n,ct;
cin>>t;
while(t--)
{
ct=0;
cin>>s;
n=stoi(s);
for(i = 0; i < s.length(); i++)
{
if(n%(s[i]-'0') == 0 && s[i] != '0')
{
ct++;
}
}
cout<<ct<<endl;
}
}
if(n%(s[i]-'0') == 0 && s[i] != '0')
For a start, this is in the wrong order. You want to ensure it's not '0' before attempting a division-type operation. The reason for this is that C++ uses short-circuiting operation so that in the following code, if xyzzy is false, plugh will never be evaluated:
if (xyzzy && plugh)
If xyzzy is a "dangerous" operation that shouldn't happen when plugh is false, you'll get into trouble.
In your specific code, what you have will first try to evaluate the division before checking the digit. So, it will allow division by zero to be performed, not really a good idea. It would be safer to use:
if ((s[i] != '0') && (n % (s[i] - '0') == 0))
There's a few other minor issues in your code such as signed/unsigned comparisons, or the not-really-necessary use of long long(a), or not following the guideline of declaring variables where they're needed rather than at some point long before then (such as at the top of the function).
But it's the order of the sub-expressions in the if statement that's most likely your immediate issue.
(a) Just how long do you expect these strings to get? :-)
The issue I am running into is that when I run the code, it adds correctly but when it comes time to run the if or else statement, nothing happens. It always just ends.
#include <iostream>
using namespace std;
int main()
{
int firstNumber;
int secondNumber;
int sum;
char restart = 1;
while (restart == 1)
{
cout<<"Welcome to My Calculator!\n\n";
cout<<"What is the first number you would like to add?\n\n";
cin>>firstNumber;
cout<<"What is the second number you would like to add?\n\n";
cin>>secondNumber;
cout<<"Wonderful! Getting together your result now....\n\n";
sum = firstNumber + secondNumber;
cout<< sum<<endl;
cout<<"If you have another question, just enter 1, if not press 2!\n\n";
cin>>restart;
if (restart == 1)
{
cout<<"No problem!\n\n";
}
else
{
cout<<"Goodbye!";
}
}
return 0;
}
char restart = 1;
if (restart == 1)
needs to be
char restart = '1';
if(restart == '1')
There is a difference between 1 and '1'. When you want to compare chars you use the '' marks.
Also, you should also always initialize your ints
This is because C++ doesn't automatically set it to zero for you. So, you should initialize it yourself:
int sum = 0;
An uninitialized variable has a random number such as 654654,-5454, etc. (If it doesn't invoke undefined behavior when reading it)
As you might be able to tell im trying to introduce a counter for how many consonants there are in a certain string, but the output is always 50, unless spaces are involved.
int main(){
char input[50];
int total = 0;
cout << "Enter a string of no more than 50 characters\n";
cin.getline(input,50);
for(int n = 0; n<50;n++){
if(input[n]!='a',input[n]!='A',input[n]!='e',input[n]!='E',input[n]!='i',input[n]!='I',input[n]!='o',input[n]!='O',input[n]!='u',input[n]!='U',input[n]!=' ')
total++;}
cout << total << endl;}
According to wikipedia, comma operator is defined as follows
In the C and C++ programming languages, the comma operator
(represented by the token ,) is a binary operator that evaluates its
first operand and discards the result, and then evaluates the second
operand and returns this value (and type).
In your case, you should use logical AND (&&) instead of comma operation.
More efficiently, you may rewrite your code like this
char c = tolower(input[n]);
if (c >= 'a' && c <= 'z' && c != 'a' && c != 'e' && c != 'i' && c != 'o' && c !='u')
total++;
It will include all the cases.
As everyone has clearly explained, you need to use && operator to ensure all conditions are checked.
if(input[n]!='a' && input[n]!='A' && input[n]!='e' && input[n]!='E' && input[n]!='i' && input[n]!='I' && input[n]!='o' && input[n]!='O' && input[n]!='u' && input[n]!='U' && input[n]!=' '){
total++;
}
One recommendation to avoid multiple checks:
Extract the character to a variable converted to lower or upper case.
char c = input[n] | 32;
Regarding the ',' used; this program might provide more insight along with the WIKI shared :
void main(){
int a=-1;
if(++a,a, a++){ // Works like "||"
printf("Yes %d",a);
}else {
printf("NO %d", a);
}
}
Output: NO 1
The question requires to output the input following the rules below:
1. if the input character is between A-Z, or a-z, the out put character would be
the following letter, abc-->bcd
2. if the input is Z or z, the output would be A or a, z->a,Z->A
3. if the input is space, then it remains the same
4. if the input is anything else, increment its ascii value by 1, and print.
This is an example:
input: abcZ ]
output: bcdA ^
Here is my code:
#include <iostream>
using namespace std;
int main()
{//use ASCII to get a code for input by changing a to b, b to c....z to a, space remain the same, everything else ++
char c,d;
int i=0;
for(i=0;;i++)
{
if (('A' <= (c=cin.get()) < 'Z')||('a' <= (c=cin.get()) < 'z'))
{
d=c+1;
}
else if(c=cin.get()==32)// ascii value of space is 32
d=c;
else if((c=cin.get())=='Z')
d='A';
else if((c=cin.get())=='z')
d='a';
else
{
c++;
d=c;
}
cout<<d;
}
cout<<endl;
return 0;
}
This is the output:
What I am thinking is that ♂ is the output of enter key, but I dont want an output for enter key.
space,Z and z don't convert properly either.
Can anyone help me with the code?Thank you.
You've got a bunch of problems here. Here are some hints:
1) Call cin.get() just once per loop iteration. That is:
for (...)
{
c = cin.get();
// do not call cin.get() beyond this point.
// use the c variable instead
...
}
2) Be careful around your compound conditionals.
Instead of: ('A' <= c < 'Z'), you really want: ('A' <= c && c < 'Z')
3) Add another condition to check for 10. This is the code for the line feed character. If this is detected, just do a cout << endl
There are a number of ways to simplify the logic here too. Keep trying!
I'm trying to write a c++ program to read a line of inputed code (eg The Hat is flat) and get the program to output the number of capital letters the code has. (in this example it would have to output 2). I have written a piece of code using cin.get() but my code is not entering the while loop. Please help me. and please change 'my code' only.
#include <iostream>
using namespace std;
int main ()
{
char y = 0;
int capitals = 0;
int flag = 0;
cout << "Enter your line of words followed by a fullstop: ";
while (!flag == -1)
{
cin.get(y);
if ((y >= 65) && (y <= 90))
{
capitals = capitals + 1;
}
if (y == 46)
{
flag = -1;
}
}
cout << "Number of capitals equal is this line is: " << capitals << endl;
return 0;
}
flag is initialised to 0, !flag is therefore 1, and your while loop is never entered.
You could fix this with:
while (!(flag == -1))
or
while (flag != -1)
Instead of !flag == -1 use flag != -1.
! is a unary logical NOT operator, and !0 is 1, not -1.
Have you tried this?
while (!(flag == -1))
or
while (flag != -1)
I think you need while (!(flag == -1)) ... but it would be better to write while (flag != -1). The ! operator has higher precedence so it is evaluated before ==.
Also you should try to use isupper() to test for uppercase -- don't reinvent the wheel.
I think the error is operator precedence. !flag == -1 is equivalent to (!flag) == -1. Try flag != -1.
If you'll forgive a probably-too-complete answer to a probable homework question:
while (cin >> y && y != '.')
capitals += (bool)isupper(y);