As of late, I've been doing a complete review of C++ and came across a code snippet containing the following:
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a)
{
b+=a;
}
cout << b << endl;
return 0;
}
The code snippet seems very straightforward: it puts input from the console into a and adds this onto b, for as long as valid input is presented. However, whenever I try to run this program with an input of integers, seperated with spaces, it gives a very large negative integer (-1218019327 being the most recent result, when the entire input only consisted of the number '1'). Only when I modify the code does it give correct output:
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a)
{
cout << a << endl;
b+=a;
}
cout << b << endl;
return 0;
}
Why does adding a cout statement change the result of my code so thouroughly (yet positively)?
Both programs result in undefined behavior, you did not initialize b. Try:
int b = 0;
You have to initialize b=0;. Or b will give you garbage value.
#include <iostream>
using namespace std;
int main()
{
int a, b=0;
while (cin >> a)
{
cout << a << endl;
b+=a;
}
cout << b << endl;
return 0;
}
By pressing ctrl-z you will get the value of b.
Related
Please consider the following code:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
int a, b;
cout << "Enter two integer: ";
cin >> a >> b;
if (a > b) {
int temp = a;
a = b;
b = temp;
}
cout << a << "<=" << b << endl;
}
The above code yields the minimum of the two inserted numbers. Can anyone explain how the if block works?
It's the idiomatic way of swapping two numbers.
There are more efficient ways: to exploit those use std::swap instead.
(The statement int temp=a; sets the variable temp to the value of a. The statement a=b; sets a to the value of b. Finally, b=temp; sets b to temp which was the original value of a. The overall effect therefore is to exchange the values of a and b.)
I have those two pieces of code as my home assignment. The code looks all fine to me, but it won't print out what I want, no matter what. In fact, the console output remains completely empty.
The first program is supposed to print out all numbers that fulfil the ladna() function requirements and are between 1 and a:
#include <iostream>
using namespace std;
int a;
int i = 1;
bool ladna(int a)
{
if((((a>>4)*5+a*2)%3)==1)
return true;
else
return false;
}
int main()
{
cerr << "Podaj liczbe: " << endl;
cin >> a;
while (i <= a){
if (ladna(a)){
cout << i << " ";
}
i++;
}
}
the ladna() function is premade and I have to use it as is.
I tried changing while into do...while and for, didn't help. Doesn;t work with cerr either.
The second code has to print out all the natural divisors of number a.
#include <iostream>
using namespace std;
int main()
{
int a;
cerr << "Podaj liczbe" << endl;
cin >> a;
for (int i = 0; i >= a; i++){
if (a % i == 0){
cout << i << endl;
}
}
return 0;
}
Doesn't work either.
To me it looks like both pieces of code have the same issue, because they are written in the same way, based on the same principle, and the error is the same. Hence my assumption, that the cause is the same as well.
Unfortunately, for the love of me, I simply can't see what said error is...
For the first code:
I think you should call ladna function with i, like ladna(i)
For the second code:
In for it should be i<=a
'%' is the modulo operator, during the execution of (a%i) you divide a with i and take the remainder, since i start with zero you will get "Floating point exception (core dumped)" due to division by zero. So, for should start with 1. This should work:
for (int i = 1; i <= a; i++){
if (a%i == 0){
cout << i << endl;
}
}
When I run this program and input, for example, the number 7, the final cout command only works occasionally. Otherwise, the program exits successfully but the result is not printed. Why is this happening?
#include <iostream>
#include <cmath>
double treble(double);
int main()
{
using namespace std;
cout << "Enter a number:" << endl;
double numways;
cin >> numways;
numways = treble(numways);
cout << "Your number trebled is: " << numways << endl;
return 0;
}
double treble(double n)
{
return n * 3;
}
You should put using namespace std; outside of all function declarations, right under your #include directives. Also, when you say it's not printing, is it that the console is closing before displaying your result? In that case, I would advocate using a simple cin to "pause" the program. You can do it exactly as #Nihar says, though I might suggest using a string instead of an int so that it doesn't break if you accidentally type something other than an int.
Something like this:
#include <iostream>
#include <cmath>
using namespace std;
double treble(double);
int main(){
cout << "Enter a number:" << endl;
double numways;
cin >> numways;
numways = treble(numways);
cout << "Your number trebled is: " << numways << endl;
string foo;
cin >> foo;
return 0;
}
double treble(double n){
return n * 3;
}
try with this => put
int temp;
cin>>temp;
before return 0; to pause the program, because the execution finished (successfully) before the last output could be written to the console.
First: I'm begginer. :) I've got a problem with my c++ code.
#include <iostream>
using namespace std;
int main()
{
int a,b;
do {
cout << "a= ";
cin >> a;
if (a<=0) {
cout << "This number is not positive!\n";
}
}
while (a<=0);
do {
cout << "b= ";
cin >> b;
if (b<=0) {
cout << "This number is not positive!\n";
}
}
while ((a==b) and (b<=0));
}
Have you got any ideas?
Thanks!
It is not possible for that condition to be true. We already know that a is positive, so it can't both equal b and b be negative.
Sounds to me like you want or instead. This would mean that b also has to be positive and must not be the same as a. Note that it is typical to use && instead of and and || instead of or:
while ((a==b) || (b<=0));
Think about it like this: we can't to continue asking for b if they input is negative or if the input is the same as a.
I've written a small program in C++ that prompts the user for input, the user gives a number, then the computer displays the number reversed.
For example: 17 becomes 71. 123 becomes 321.
This is the program:
#include <iostream>
#include <string> //for later use.
using namespace std;
int rev(int x)
{
int r = 0;
while(x)
{
r = (r*10) + (x%10);
x = x/10;
}
return r;
}
int main()
{
int nr;
cout << "Give a number: ";
cin >> nr;
rev(nr);
cout << nr;
return 0;
}
The final result of the program: prints the same number, function has no effect. What am I doing wrong? I tried several solutions but to no avail.
You need to change rev(nr); to nr = rev(nr);
or alternately change your function to:
void rev(int& x)
{
int r = 0;
while(x)
{
r = (r*10) + (x%10);
x = x/10;
}
x = r;
}
You're doing it right, but you're not grabbing your return value (the reversed value).
To solve this, just assign or print the return value:
cout << rev(nr);
or
nr = rev(nr);
cout << nr;
While probably not in the intended spirit, the simple answer is that if you're only going to display it in reverse, you can cheat and just work with a string:
std::string input;
std::cin >> input;
std::cout << std::string(input.rbegin(), input.rend());
You're not actually using the value that rev returns. You're just using the value nr which you pass to rev, and since you don't pass by reference, rev isn't being affected locally.
What you want to say is:
int nr;
cout << "Give a number: ";
cin >> nr;
int result = rev(nr);
cout << result;
return 0;
There is a std::reverse function in the STL, which works with collections.
#include <algorithm>
#include <iostream>
#include <string>
int main() {
long int i = 0;
do {
std::cout << "Gimme a number: " << std::endl;
} while (not (std::cin >> i)); // make sure it *is* a number
std::string display = std::to_string(i); // C++11
std::reverse(display.begin(), display.end());
std::cout << display << "\n";
}