Has anyone taken the C++ test on proveit.com ? I've done it a few times, and there are always questions that I get wrong, but I can't help but assume it's the site and not me. The only example I can think of off the top of my head (its been a while) is
What is the value of x after the operation?
int x = 5;
++x;
And it gives a few answers, one of which being 6. I don't see how it could be anything but. Just to be sure I would compile the code and still get the same answer, but the test would tell me I'm wrong. Wondering if anyone has any experience with this test/site.
Perhaps the test read what will this output:
int x = 5;
std::cout << x++;
And the question was:
What will be the output?
Because when you do this x will change value but will display 5 from cout because x is incremented after the original value of x is displayed. Otherwise, it should equal 6 in your case. (You said it had been a while...)
Strictly speaking this is implementation dependent, because there might an overflow here. But for all practical purpose, yes, the value of x should be 6 when the code is done execution.
You probably have an answer by now, but hopefully this will help someone else. Depending on how the question is stated you may have got the wrong answer:
int x = 5;
cout << x++ << endl;
cout << x << endl;
would display 5 and 6 because x is incremented AFTER it has been outputted. However,
int x = 5;
cout << ++x << endl;
cout << x << endl;
would display 6 and 6 because x is incremented BEFORE it has been outputted.
Related
This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 2 years ago.
I'm a newbie in the programming world, and i've decided to start with C++ code a few days ago as my first programming language.
I just started to read an online course which i'm guiding on (and aply while i'm reading it).
The course in question assigns a serie of small optional exercises, wich go hand in hand with the topic that is being dealt at that moment.
One of this optional exercises is: "Create a program that multiplies two whole numbers in the following way: it will ask the user for a first whole number. If the number that you type is 0, it will write on the screen "The product of 0 by any number is 0". If a number other than zero has been entered, the user will be prompted for a second number and the product of both will be displayed."
How it says, I did my best to coding that program.
The code of what I did is:
#include <iostream>
using namespace std;
int main ()
{
int a;
int b;
int solve;
cout << "Enter a number: ";
cin >> a;
if (a!=0)
{
cout << "Enter another number: ";
cin >> b;
}
if (b!=0)
{
solve = a * b;
cout << "The result of your operation is: " << solve << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
return 0;
}
So, I press F9 to compile, then F10 to run it.
I proceed to testing it.
I put and different number from zero, I put another one. Throws me a multiplication of both. Nice.
I put and different number from zero, I put another one that actually its zero. Throws me the message of "else" order. Nice.
**BUT
I put and number equal to zero, and throws me the message of the "cout" order of "if (b!=0)".**
I didn't really know what I had done wrong, so, I ask for help from a friend who has some more experience than me, and tells me that actually it's nothing wrong. In fact, he proved me sending to me a screen cap of his Dev C++ with my code in it, and how it runs just how it had to be.
Then, I opened an online compiler (https://www.onlinegdb.com/online_c++_compiler#) to get down my doubts, and yes, there runs correctly too.
So, here's my question?
What's the problem? Why that happens?
I have the DevC++ 5.11 TDM-GCC 4.9.2, and I'm using the deafult compiler.
Please, I would like some of help, I feel more comfortable compiling in PC than online, it's more quick.
Thank you anyway for reading until here.enter image description here
The reason why this happens, as #TrebledJ mentioned, is that you've not initialised the variable b. So you can get over this problem just by initialising b to any value of your choice (preferrably 1 or 0 for simplicity). But there's a workaround if you don't want to initialise the values. I would transform your code to something like this:
if (a!=0)
{
cout << "Enter another number: ";
cin >> b;
if (b!=0)
{
solve = a * b;
cout << "The result of your operation is: " << solve << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
Basically, what I'm doing is, if the value of a after input is 0, I'm not running the part for taking the user input of b.
I'm confused at this example:
int x = 5;
if (x==5) cout << x; // output 5
if (x==6) cout << x;
if (x=6) cout << x; // output 6
x = 0;
if (x=0) cout << x;
x = 5;
if (x-5) cout << x;
if (x-6) cout << x; // output 5
I understand first if (x==5), but why does it output 6 at if (x=6) when x = 5, and why won't it output 0 in if(x=0)
if (x=6)
means not comparison, but assignment. You assign 6 to x and the return value of the expression is 6, which is not 0 so it gains true.
similar with if (x=0) The expression x=0 gains 0 so it means if(0)
The thing about computers is that they're extraordinarily literal. A missing semicolon, or an added character, can completely change a program's function. So you need to be just as careful as a computer when working wth programs.
As #juanchopanza alluded to, there is a difference between == and = - and you already know what it is.
This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Closed 6 years ago.
I can not grasp the concept as to how these statements produce different values. As far as I know x+=1, means x = x + 1. I also know that x++ should be equivalent to x + 1.
I've also searched this topic and found posts which ask the same question, those posts are answered usually by stating the statements/expressions are the same, but the different result was due to another code mistake. With the example I will provide I don't see how there is a code mistake so please explain, thank you.
int x = 0;
x++;
x should be 1 at this point because x++ adds 1 to x.
So why is it that if I assign x to 0, and then proceed to code "cout << x++;" I get a value of 0 on the screen?!. How does x++ become 0 if x++ is equal to x+1 and if x is 0 then 1+0=1? I've been told its due to ++ being put after x, but why does that matter if dealing with addition 1 + 0 is the same as 0 + 1?
cout << x++; outputs the value of x before the increment as you are using the postfix increment operator.
cout << ++x; would do what you expect.
There are two forms of the ++ operator: prefix and postfix. You're using the postfix form.
x++ returns the current value of x, then increments it. When you use cout << x++, it prints x then increments it.
++x does what you want: it increments x then returns it. cout << ++x will give you what you want.
x++ simply returns x and then increases x by one.
So cout << x++ in your example would be the equivalent of cout << x; x = x+ 1;
That's post-increment, which means that it will execute the variable as-is, and then add to the variable.
Had you tried the pre-increment ++x, then it would add to the variable, and execute the variable as-is (which is now incremented).
Therefore, int x = 0; x = x + 1; cout << x; and int x = 0; x++; cout << x++; will all print 1.
Cout << x++ will first print x and then increase it, x++ is post increment so after the operation x is increased. Instead ++x will do the opposite. And also if you cout some expression like x+1 the expression will be evaluated before printing
x++ is a post increment, which means that it increments after it has run the current statement.
In contrast, ++x executes the current statement before executing the increment.
Hope this helps.
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 8 years ago.
Improve this question
so I just started learning programming with C++ and I'm currently messing with basic console programs. I wanted to make a little spam program. here's the code :
#include <iostream>
#include <string>
using namespace std;
string a;
int b;
void repetition(){
cout << "Please enter the number of time you want the text to be spammed" << endl;
cin >> b;
}
void text(){
cout << "Please enter the text you want to spam." << endl;
cin >> a;
for(;b == 0;){
cout << a << endl;
b - 1;
}
}
int main()
{
cout << "Welcome to your auto-spammer!!" << endl;
repetition();
text();
return 0;
}
I'm getting a warning saying "statement has no effect" for my for statement at line 20. I wanted to know why and how I could fix this. Thank you.
The for loop executes while the second statement is true. So unless you enter 0, it will never execute.
The warning is for b - 1; . This reads the value of b, subtracts 1, and does nothing with the result. You probably meant b = b - 1; (which can also be written as b -= 1;, or --b;).
I'm guessing this is line 20:
b - 1;
That line by itself does nothing. The result of b-1 is never assigned to anything.
Try --b, which will decrement b by 1 and re-assign the result of that operation to b.
In text(), b-1 indeed does nothing, you probably meant --b. The first returns an rvalue which is then discarded, while the second decrements b by one and results in b (though you should look up the difference between --b and b-- to understand how that statement actually works). That said, the more colliquial way to do it is like this:
for(; b > 0; --b) //Also keep in mind that the second section of a for statement
//is the continue condition, not exit
cout << a << endl;
You want to do print the text N number of times, so the proper loop to use is:
for (int i=0; i < b; i++)
cout<<a<<endl;
Modifying b is generally not a good idea, you might need the value the user entered later on.
I'm still very new to C++ still and decided to make a fibonacci sequence. It worked (Woo!) but it doesn't work as well as I would like it to.
what I mean by that is say for example I told my program to count the first 10 terms of the sequence I will get
"0, 1, 1" and then I have to press enter for each additional number until it hits ten in which case the program returns 0 and ends.
How do I get the program to display all the numbers I want to without hitting enter for each additional one?
Here is my script:
#include <iostream>
using namespace std;
int main()
{
int FibNum;
cout << "How many numbers of the Fibonacci Sequence would you like to see? \n\n";
cin>> FibNum;
cin.ignore();
int a = 0;
int b = 1;
int c = 2;
cout << "Fibonacci Sequence up to " << FibNum << " terms.\n\n";
cout << a << "\n" << b << "\n";
for (int c = 2; c < FibNum; c++) {
int d = a + b;
cout << d;
cin.ignore();
a = b;
b = d;
}
}
Thanks in advance for any help!
P.s. Also if you notice anything terrible I'm doing please feel free to correct me, I'm very aware I'm probably doing a lot wrong, I'm just trying to learn. :]
A few things:
1) Remove int c = 2; as you're re-defining c inside the for loop.
2) Drop the line cin.ignore();: in your for loop: that will fix your "enter" problem; that line waits for some input then ignores it.
3) Put some white space in your output: e.g. cout << d << ' ' so your numbers are separated.
4) [Acknowledge vincent_zhang] Consider moving to uint64_t as your data type for a, b, and d. This is a standard type in C++11. It's a 64 bit unsigned integer type; adequate for a large number of terms.
and a small thing, bordering on personal opinion,
5) Use ++c instead of c++ as the former will never run slower as, conceptually at least, post-increment has to take a copy of the original value.
Besides the previous answers,
To better format the output, add white space by changing this
cout << d;
to
cout << d << " ";
You may want to change the type of a, b and d from int to double to prevent overflow.
(If you let FibNum=100 in your code, you should be able to observe overflow, meaning that you are going to get some incorrect numbers toward the end of the sequence.)
Move cin.ignore() out of the loop then you dont need to enter to print all the 10 numbers of Fibonacci series