c++ help me to understand why there is no output [closed] - c++

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 3 years ago.
Improve this question
this program is simple :
1)take an input string .
2) convert it to long .
3) print convert result.
expected an output,but nothing found.
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
string ch;
scanf("%s",ch);
long l=stol(ch);
printf("%l",l);
return 0;
}

Here's how it's done with C++ I/O. There's very little reason for using C I/O in a C++ program.
#include <iostream>
#include <string>
int main()
{
std::string input;
std::cin >> input; // take an input string
long lval = stol(str); // convert to long
std::cout << lval << '\n' // print the result
}
Now this stuff would be covered in the first chapter of any C++ book. A good book will greatly increase how quickly and how well you learn C++.

Related

'cin' was not declared in this scope [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 months ago.
Improve this question
I run my code and it gives an error 'cin' was not declared in this scope
My code
#include <bits/stdc++.h>
int m,a,b,c,d,e,f;
main()
{
cin>>m>>a>>b>>c>>d>>e>>f;
double g=a%b;
double h=c%d;
double k=e%f;
if (g<h && g<k){
int i=g;
}
if (h<g && h<k) {
int i=h;
}
if (k<g && k<h) {
int i=g;
}
double s=i*m;
cout<<s;
}
`
i think i wrote it right,help me
Use C++ standard library includes
#include <iostream>
then
std::cin
and
std::cout
If you do this, then you are writing portable and easy to read C++.
Even better, check that the data have been successfully read:
if (std::cin >> m >> a >> b>> c>> d>> e >> f){
// success - go on from here
}

Scope not defined c++ need int inout [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 8 years ago.
Improve this question
#include <iostream>
#include <stdio.h>
int main(void)
{
printf("Hello, CS 101! \n");
int age{
printf("How old are you? \n")
};
printf("OK, you are",scanf(%i,&age),"years old.");
return 0;
}
Our assignment needs to use scanf and printf to find out how old the user is, then to print a statement stating how old they are.
Our professor didn't go over syntax and gave us the code in class, but I was too far from the board to see. This is what I got on my own, but I have not found good coding resources for c++.
I get the error scope not defined, can someone help me. Also, if anyone has good resources for learning from scratch that would be great. This is my first time coding, thanks.
I fixed your code. Hope it is what you are after:
#include <iostream>
#include <stdio.h>
int main(void)
{
printf("Hello, CS 101! \n");
// declare age as int
int age;
// ask for age
printf("How old are you? \n");
// get user input into age
scanf("%d", &age);
// display entered age
printf("OK, you are %d years old.\n", age);
return 0;
}

sqrt function doesn't evaluate correct value [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 8 years ago.
Improve this question
I'm newbie in C++ and I'm trying to evaluate a square root. I've written the following:
#include <math.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
double a;
a=sqrt(2.0);
printf("Square root from 2 is %d\n",a);
return 0;
}
But output is Square root from 2 is 1719614413. I really don't understood this. Please explain me.
You are using C (which mostly compiles as C++ as well), not C++ and you made a mistake doing so. You can either learn C or use C++, where it's harder to make those mistakes:
#include <cmath>
#include <iostream>
int main()
{
double a = std::sqrt(2.0);
std::cout << "Square root from 2 is " << a << std::endl;
return 0;
}
Additional explanation:
printf("Square root from 2 is %d\n",a);
This prints a string and expects an integer (%d) to be passed. You passed a double, wich comes out as garbage. You can use (%lf) for doubles.
If you still want to stick to our example, please consider using %f rather than %d:

Why does writing into my string not work? [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 9 years ago.
Improve this question
The following Code is not working. I get an error at the command cin >> h. What am I doing wrong?
#include <iostream>
using namespace std;
int main()
{
string h = " ";
cout << "hi" << endl;
cin >> h;
cout << h << endl;
system("pause");
return 0;
}
Random guessing:
You forgot to #include <string>
You forgot to include <string> and C++ punished you for that.
Ah, but every man and his dog should know that by not including <string>, you were using the default >> operator, that has well know issues with strings.
C++'s "leave the progammers free to shoot themselves in the foot" philosophy at its best.
C++ lore tells the unfortunate wandereds should use getline instead of cin >>, but there have been heated debates among scholars on this fine doctrine point.

Returning boolean value does not print anything and doesn't show any compiler errors [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 1 year ago.
Improve this question
This is a function to check a number for a power of two. Although the compiler does not print anything, it's also not showing any errors. Please let me know if the logic is correct or not.
Here is my code:
#include <bits/stdc++.h>
using namespace std;
bool isPowerofTwo(long long n)
{
// Your code here
for (int i = 1; i <=n; i<<1)
{
if(i==n){
return true;
}
}
return false;
}
int main()
{
cout << isPowerofTwo(2);
return 0;
}
The expression i<<1 in the third statement (the "iteration expression") of your for loop doesn't actually do anything (that is, it doesn't modify the value of i). You need, instead, i<<=1 (or, in 'long form', i = i << 1).
Also, please read: Why should I not #include <bits/stdc++.h>?. In your code, the only standard header you need is #include <iostream>. Another good post to read is: Why is "using namespace std;" considered bad practice?.