What is the problem with the last two statements in the code?
#include <iostream>
using namespace std;
int main()
{
cout << "2 + 4 = " << 2 + 4 << endl;
cout << "2 * 4 = " << 2 * 4 << endl;
cout << "2 | 4 = " << 2 | 4 << endl;
cout << "2 & 4 = " << 2 & 4 << endl;
What should I do to fix this?
What is the problem with the last two statements in the code?
Operator precedence. | and & have lower precedence than <<, so cout << "2 & 4 = " << 2 & 4 << endl; gets parsed as (cout << "2 & 4 = " << 2) & (4 << endl;).
What should I do to fix this?
Put parens around 2 | 4 and 2 & 4.
Put the expression in parentheses. The << operator is taking precedence over the bitwise operators.
#include <iostream>
using namespace std;
int main()
{
cout << "2 + 4 = " << 2 + 4 << endl;
cout << "2 * 4 = " << 2 * 4 << endl;
cout << "2 | 4 = " << (2 | 4) << endl;
cout << "2 & 4 = " << (2 & 4) << endl;
return 0;
}
Related
Hello I need help with this tracing problem, here here is a copy of the problem:
What is the output of the following program segment?
int u = 4, v = 3;
one(u, v);
cout << u << " " << v << endl;
cout << two(u, v) << " " ;
cout << u << " " << v << endl;
void one(int &x, int& y){
int a;
a = ++x ;
x += y++;
y = ++a;
}
int two(int s, int &t){
int b;
b = s – t;
s += t + b ;
t += 4 * b;
cout << s << " " << t << " " << b << endl;
return ( b ) ;
}
I managed to find the first output of function One, then I plugged it into function Two to find its output. But function Two returned the cout instead of the return (b), can anyone show me what I am doing wrong, any help would be greatly appreciated! Here is a copy of the output after plugging it into visual studios:
Output:
8 6
16 14 2
2 8 14
The relevant fragment is equivalent to the following, with the step-by-step breakdown in comments.
cout << u << " " // prints 8
<< v // prints 6
<< endl; // ends first line "8 6"
int n = two(u, v); // calls 'two' which prints the second line "16 4 2" and returns 2
cout << n // prints 2 which is the value returned by 'two' at the previous step
<< " " ; // prints a space but does not end the line
cout << u << " " // prints 8 next on the third line
<< v // prints 14
<< endl; // ends third line and prints "2 8 14"
I'm trying to do the following assignment in a c++ book.
After running this:
#include <iostream>
using namespace std;
int main()
{
double first_arg;
double second_arg;
cout << "Enter first argument: ";
cin >> first_arg;
cout << "Enter second argument: ";
cin >> second_arg;
cout << first_arg << " * " << second_arg << " = "
<< cout << first_arg * second_arg << "\n";
cout << first_arg << " + " << second_arg << " = "
<< cout << first_arg + second_arg << "\n";
cout << first_arg << " / " << second_arg << " = "
<< cout << first_arg / second_arg << "\n";
cout << first_arg << " - " << second_arg << " = "
<< cout << first_arg - second_arg << "\n";
I get some unexpected results. Like this result copied straight from the windows cli:
Enter first argument: 7
Enter second argument: 9
7 * 9 = 0x6fcc43c463
7 + 9 = 0x6fcc43c416
7 / 9 = 0x6fcc43c40
7 - 9 = 0x6fcc43c4-2
I'm using the latest version of codeblocks with the default compiler settings. Thanks.
cout << first_arg << " * " << second_arg << " = "
<< cout << first_arg * second_arg << "\n";
You have two cout in one line since there is no semicolon on line 1
To fix this either get rid of the second cout or add a semicolon at the end of the first line on each cout statement.
If you look at the last 2 digits of each answer you will see the answer you wish to get so its still printing out the answer you want its just after the pointer to cout.
This question already has answers here:
What are the barriers to understanding pointers and what can be done to overcome them? [closed]
(28 answers)
Closed 8 years ago.
Could anyone explain the difference between these lines?
Where: char ** d_array, I just can't predict the outcome of these lines. Never really understood the pointers in C++, and didn't find any tutorial where they'd explain such lines yet.
Thanks in advance!
• d_array + 2
• *(d_array+ 2)
• *(d_array+ 2) + 1
• *(*(d_array+ 2) + 1)
• d_array[1][1]
• *(d_array[1]+1)
#include <iostream>
using namespace std;
int main() {
const char* d_array[] = { "one", "two", "tree", "four" };
cout << "1: " << d_array + 2 << endl;
cout << "2: " << *(d_array+ 2) << endl;
cout << "3: " << *(d_array+ 2) + 1 << endl;
cout << "4: " << *(*(d_array+ 2) + 1) << endl;
cout << "5: " << d_array[1][1] << endl;
cout << "6: " << *(d_array[1]+1) << endl;
}
Output:
1: 0xbfee7208
2: three
3: hree
4: h
5: w
6: w
http://ideone.com/XjVYsg to see it in action
void printAst(int x)
{
for( int i = 0; i < x; i++)
{
cout << "*";
}
cout << " (" << x << ")" << endl;
}
void printHisto(int histo[])
{
//cout.precision(3);
int count = 0;
for(double i = -3.00; i < 3.00; i += 0.25)
{
cout << setprecision(3) << i << " to " << i + 0.25 << ": " << printAst(histo[count]) << endl;
// cout << setw(3) << setfill('0') << i << " to " << i + 0.25 << ": " << histo[count] << endl;
count ++;
}
}
I want my output to be formatted like this, so I used setprecision(3), which also does not work.
-3.00 to -2.75: (0)
-2.75 to -2.50: * (1)
-2.50 to -2.25: * (1)
-2.25 to -2.00: * (6)
-2.00 to -1.75: ***** (12)
So instead it is formatted like this
-3 to -2.75: 3
-2.75 to -2.5: 4
-2.5 to -2.25: 5
-2.25 to -2: 0
-2 to -1.75: 0
The main problem however, is that when I try to call printAst on to histo[count]. This is what is causing this error. PrintAst is used to print the asteriks, histo[count] provides the amount of asteriks to print.
cout << setprecision(3) << i << " to " << i + 0.25 << ": " << printAst(histo[count]) << endl;
You seem to have a misunderstanding about how chaining << works in streams.
cout << 42 looks like an operator expression with two operands, but it's really a call to a function with two parameters (the name of the function is operator<<). This function returns a reference to the stream, which enables the chaining.
An expression like this:
cout << 1 << 2;
is equivalent to this:
operator<<( operator<<(cout, 1), 2);
Now, the problem is that a parameter to a function can't be void but that's what printAst returns. Instead, you need to return something that can be streamed - in other words, something that operator<< is already overloaded for. I'd suggest std::string:
std::string printAst(int x);
{
std::string s = " (" + std::string(x,'*') + ")";
return s;
}
You can read more about operator overloading.
I need to be able to have spaces come up between each number. Here is my code. Any help would be awesome! This app allows you too have 6 rows of 6 numbers generated for your insta pick numbers between 1 - 49, it has to pick two rows of 6 numbers, 1 - 49 for twist and 1 row of 6 numbers for tag.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
{
cout << "*** LOTTO MAX INSTA PICK ***" << endl;
cout<< " " << endl << endl;
}
{
cout << "Your Insta Pick Numbers" << endl;
cout<< " " << endl << endl;
}
for (int counter = 1; counter <= 24; ++ counter)
{
cout << setw(1) << (1 + rand() % 49);
if (counter % 6 == 0)
cout << endl;
}
{
cout<< " " << endl << endl;
cout<< " " << endl << endl;
}
{
cout << "Your Twist Numbers" << endl;
cout<< " " << endl << endl;
}
for (int counter = 1; counter <= 12; ++ counter)
{
cout << setw(1) << (1 + rand() % 49) , " ";
if (counter % 6 == 0)
cout << endl;
}
{
cout<< " " << endl << endl;
cout<< " " << endl << endl;
}
{
cout << "Your Tag Numbers" << endl;
cout<< " " << endl << endl;
}
for (int counter = 1; counter <= 6; ++ counter)
{
cout << setw(1) << (1 + rand() % 12);
if (counter % 6 == 0)
cout << endl;
}
{
cout<< " " << endl << endl;
cout<< " " << endl << endl;
}
{
cout << "Thank you for playing!! please check ticket a year minus a day from date of purchase" <<endl;
}
};
You almost had it when you did
cout << setw(1) << (1 + rand() % 49) , " ";
but that doesn't do what you think it does. It evaluates two expressions, separated by a comma - cout << setw(1) << (1 + rand() % 49) and " ". The first does the setw and prints (1 + rand() % 49), and the second one just evaluates to itself and has no effect. Remember that << is the output operator for cout, so you just need to change the comma to a <<:
cout << setw(1) << (1 + rand() % 49) << " ";
The same thing goes for the other places you are printing numbers.
Use cout << setw(1) << (1 + rand() % 49) << " "; in your loop. (Note that , was replaced with <<.