How to access the variable value outside loop in C++ - c++

#include <iostream>
using namespace std;
int main()
{
int sqdnumber;
int sqdNumber_result=0;
cout<<"Enter a number:";
cin>>sqdnumber;
cout<<"\n";
while(sqdnumber==0)
{
int remainder=sqdnumber%10;
sqdNumber_result=(remainder*remainder)+sqdNumber_result;
sqdnumber=sqdnumber/10;
}
cout<<sqdNumber_result;
}
I am trying to print sum of square of individual digit but the variable sqdNumber_result is not accessible outside loop. Could you tell me how to solve this? Thank you.

sqdNumber_result is perfectly accessible outside of the loop, and your code compiles fine.
I suspect that you got strange ideas about variable shadowing because your code outputs 0 for any (nonzero) input; that's because you got the while condition wrong: while(sqdnumber==0) won't make you even enter the loop whatever input you provide, and will get you stuck forever in the loop for zero input.
You want while(sqdnumber!=0).

Related

The While loop is not terminating even i tried decrementing t

The Problem of the Solution is that first line will contain total no. of Input and the next line will take three integer and then we have to calculate the result based on the input.So the Problem with my Solution is that the While Loop is Not Terminating.
I have Checked the code several times and thinks that this is happening because of the conditional statement and because of if-else the --t statement is not getting executed.So,what modifications should i do to make the program terminate after t input.
#include<iostream>
using namespace std;
int main()
{
int t{0};
cin>>t;
while(t)
{
double h{0},c{0},t(0);
cin>>h>>c>>t;
if(h>50 and c<0.7 and t>5600)
cout<<"10\n";
else if(h>50 and c<0.7)
cout<<"9\n";
else if(c<0.7 and t>5600)
cout<<"8\n";
else if(h>50 and t>5600)
cout<<"7\n";
else if(h>50 or c<0.7 or t>5600)
cout<<"6\n";
else
cout<<"5\n";
--t;
}
return 0;
}
I expect the Program to terminate after t input but it is not happening.
Outside the loop you have
int t{0};
And inside the loop you have
double h{0},c{0},t(0);
You have two different and distinctive variables t that shadows each other. And inside the loop when you do --t you decrement the variable inside the loop, not the one you use for the looping condition.
You solve this by using descriptive names for your variables, instead of short one-letter names.

C++ Why does this code compile?

I'm trying to understand why does the following code compile?
#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin" << cin;
return 0;
}
How is the compiler able to distinguish in the statement: "cin >> cin"?
If you run the program, you'll notice that it never waits for input.
It doesn't distinguish anything – you're right-shifting the int by its own value.
(And that value is indeterminate, so the program is undefined.)
If you increase the warning level of your compiler, you should see "Warning: statement has no effect" or something to that effect.
To add on to molbdnilo's answer:
When you are using the statement using namespace std;, you are telling the compiler that for all variables in the current scope, loop up the identifier in current scope, if not, look up in the parent scope, until the look up reaches global scope and still unable to find the identifier, then it will try to find it in the namespace you are using.
After you declared int cin, everything that is called cin in the main function will be the local int cin not the std::cin, so all of your codes are just left shifting cin and shifting "cin" by cin amount of bits.

how come my c++ code gives 2 different outputs in ideone and codeforces custom test?

ideone:https://ideone.com/Ba3Nw7
#include <iostream>
using namespace std;
int main() {
int i,n,b25,b50,temp;
cin>>n;
for(i=0;i<n;i++)
{
cin>>temp;
if(temp==25)
b25++;
else if(temp==50)
{
if(b25>0)
{
b25--;
b50++;
}
else
{
cout<<"NO";
return 0;
}
}
else if(temp==100)
{
if(b25>0 && b50>0)
{
b25--;
b50--;
}
else if(b25>2)
b25-=3;
else
{
cout<<"NO";
return 0;
}
}
}
cout<<"YES";
return 0;
}
the test case tried is:
2
25 100
the output on ideone is "NO" which is the correct answer but on the codeforces custom test it gives a "YES",why is that?
One very glaring mistake I can see is not initialising variables b25 and b50.
In your code, you are continuously incrementing and decrementing the two variables, and therefore, your answer goes wrong every time.
I would suggest:
int i,n,b25=0,b50=0,temp=0;
You might be getting a right or a wrong answer because arbitrarily any value can get stored in b25 and b50. Sometimes it may satisfy the NO condition, and at other times, a YES condition.
Had your variables been static or within file scope, they would have been initialised as 0. However, your variable is locally defined, hence its value will be indeterminate, invoking undefined behaviour.
You are using the variables b25, b50 uninitialised, but guessing from your code you assume them to be initially zero.
Their initial value can currently differ from run to run, so putting it a second time on one of those platforms may give more different results. (Unless ideone uses BSD or something where memory is initially zero by default)

Error converting int y to vector <int> any ideas?

I'm new to C++ and am not sure what's wrong. This is a task I have been given in my programmging course at uni which is meant to take user input of a vector of grades and determine whether the grade is a passing one. When I compile I end up getting an error stating q1.cpp:30:21: error: could not convert ‘y’ from ‘int’ to ‘std::vector’
Not overly sure why. Sorry about the bad formatting.
I've added the code but not sure how to wrap it.
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
int calcNumberOfPasses(vector<int> grades){
int x;
for (int i=0; i<grades.size(); i++){
cin >>grades[i];
}
cin >> x;
}
int main() {
int y;
vector<int> nGrade;
nGrade.push_back(y);
cout << "Enter how many grades you want to enter";
for (int i=0; i<nGrade.size();i++){
cin >> nGrade[i];
}
cin >> y;
if (y>=50){
cout << "this is a passing grade";
}
calcNumberOfPasses(y);
}
The function calcNumberOfPasses is expecting a parameter of type vector<int>, you are passing it a parameter of type int. That much you can work out from the error message.
You are copying an undefined value into the vector on this line:
nGrade.push_back(y); // y hasn't been initialised yet, you probably want to remove this line.
Following that you are looping over the size of the grades vector, which hasn't been initialised yet.
Chances are, you want to do calcNumberOfPasses(nGrades);.
As an aside, you should use a reference to the vector, to avoid copying it.
In summary, I would through all of this code away and start again. No offence!
A vector is a collection -- a grouping of items of some base class. It's conceptually similar to an array. What you are doing is trying to repeatedly load a single variable and then pass that into a function that expects a vector.
Try breaking down the steps of the function you're writing. You are:
Adding a single, uninitialized int to a vector.
Trying to retrieve a number to control the number of grades you want to enter.
Reading a single additional number into y.
Passing that single number into a function that expects an array.
There are numerous things wrong with this function; I think you need to map out what data needs to go where.

How do I check the input data type of a variable in C++?

I have one doubt about how to check the data type of input variables in C++.
#include<iostream>
using namespace std;
int main()
{
double a,b;
cout<<"Enter two double values";
cin>>a>>b;
if() //if condition false then
cout<<"data entered is not of double type";
//I'm having trouble for identifying whether data
//is double or not how to check please help me
}
If the input cannot be converted to a double, then the failbit will set for cin. This can be tested by calling cin.fail().
cin>>a>>b;
if(cin.fail())
{
cout<<"data entered is not of double type";
}
Update: As others have pointed out, you can also use !cin instead of cin.fail(). The two are equivalent.
That code is hopelessly wrong.
iostream.h doesn’t exist. Use #include <iostream> instead. The same goes for other standard headers.
You need to import the namespace std in your code (…). This can be done by putting using namespace std; at the beginning of your main function.
main must have return type int, not void.
Concerning your problem, you can check whether reading a value was successful by the following code:
if (!(cin >> a))
cout << "failure." << endl;
…
Also, if my memory serves, the following shortcut should work:
if (! (cin>>a>>B)) { handle error }