• 0

C++ Help Coding Problem


Question

I am using Dev-C++ and my code isn't working. When I compile it it stops at the code C=A. The error message states that I don't have a header and the program doesn't recognize C=A. This is the question and I will give you what code I have.

I do not know what I am doing wrong. Help Please.

Write a program that declares 2 constants A and B, Initialize A=1 and B=2.2, Declare an int named C and a float named D, Initialize C=A and D=B, Write statements to print C and D to the screen

Ok here is my code:

// abcddec.cpp

// example of printing C and D

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int C;

C=A;

A=1;

float D;

D=B;

B=2.2;

cout << "C\n";

cout << "D\n";

system ("PAUSE")

return 0;

}

Link to comment
https://www.neowin.net/forum/topic/660056-c-help-coding-problem/
Share on other sites

Recommended Posts

  • 0

// abcddec.cpp
// example of printing C and D

#include &lt;iostream&gt;
#include &lt;cstdlib&gt;

using namespace std;

int main()
{
int C;
int A;
A=1;
C=A;

float D;
float B;
D=B;
B=2.2;

cout &lt;&lt; "C\n";
cout &lt;&lt; "D\n";
system ("PAUSE");
return 0;
}

You forgot to declare your variables, you can't left them undeclared.

Edited by Telemachus
  • 0

Ok I declared the variables but now I get this error. Thanks :cool:

In function `int main()':

error: expected `;' before "return"

Execution terminated

The error is here:

// abcddec.cpp

// example of printing C and D

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int C;

int A;

C=A;

A=1;

float D;

float B;

D=B;

B=2.2;

cout << "C\n";

cout << "D\n";

system ("PAUSE")

return 0;

}

  • 0
Ok I declared the variables but now I get this error. Thanks :cool:

In function `int main()':

error: expected `;' before "return"

Execution terminated

The error is here:

// abcddec.cpp

// example of printing C and D

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int C;

int A;

C=A;

A=1;

float D;

float B;

D=B;

B=2.2;

cout << "C\n";

cout << "D\n";

system ("PAUSE")

return 0;

}

You should also swap around your variable assignments as you are setting C = A before you've given A its value. Similarly with D you've set it to B before assigning B a value.

  • 0

I'm surprised that this code even compiles since writing C=A before having given any value to A makes no sense at all. If you were to print out their values right now, you would see garbage values on the screen. As ViZion said, first give a value to A, then assign it to C. Same thing for D and B.

Also, your assignment asks to print C and D to the screen: they obviously don't want you to print the letters "C" and "D", but rather the variables C and D, so that what gets effectively printed is their respective values. In that case, simply remove the quotes. To print a new line, add << endl; to the cout statement.

Finally, there is no need to #include cstdlib. The only external resource you are using here is cout from iostream.

  • 0

I took out the quotes and added << endl; I got an error message. This is what I got.

// abcddec.cpp

// example of printing C and D

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int C;

int A;

A=1;

C=A;

float D;

float B;

B=2.2;

D=B;

cout << 1\n;

cout << 2.2\n;

<< endl;

system ("PAUSE");

return 0;

}

5 errors

Compiler: Default compiler

Executing g++.exe...

-g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3

In function `int main()':

error: stray '\' in program

error: expected `;' before "n"

error: stray '\' in program

error: expected `;' before "n"

error: expected primary-expression before '<<' token

Execution terminated

Ok now I switched the variables like what was suggested and instead of printing C and D I set it to print 1 and 2.2 and it works.

// abcddec.cpp

// example of printing C and D

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int C;

int A;

A=1;

C=A;

float D;

float B;

B=2.2;

D=B;

cout << "1\n";

cout << "2.2\n";

system ("PAUSE");

return 0;

}

  • 0

This worked great as an alternative Thanks:

CODE

cout << "1\n";

cout << "2.2\n";

with:

CODE

cout << 1 << endl;

cout << 2 << endl << 3 << endl;

Now this code gives me this error even if I take out "cout << "1\n"; cout << "2.2\n";" I obviosly am doing something wrong:

cout << "1\n";

cout << "2.2\n";

cout << variable << endl;

`variable' undeclared (first use this function)

(Each undeclared identifier is reported only once for each function it appears in.)

I am learning a lot here thanks. :)

  • 0

Yes I put variable in italics to indicate that you can put a variable there. So, try replacing variable with what you have declared : A, B, C, D. When you cout << A, you are effectively evaluating the expression A, which is the value of A, and sending this value into the console output stream.

  • 0
Yes I put variable in italics to indicate that you can put a variable there. So, try replacing variable with what you have declared : A, B, C, D. When you cout << A, you are effectively evaluating the expression A, which is the value of A, and sending this value into the console output stream.

Is this what you mean and thanks :)? The answer is:

1

1

2.2

2.2

A is 1 and C is A which is 1

B is 2.2 and D is B which is 2.2

// abcddec.cpp

// example of printing C and D

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int C;

int A;

A=1;

C=A;

float D;

float B;

B=2.2;

D=B;

cout << A << endl;

cout << C << endl;

cout << B << endl;

cout << D << endl;

system ("PAUSE");

return 0;

}

  • 0
To throw a spanner in the works, if you're wanting to cause a line-break, use '\n' instead of endl. endl calls flush() every time it's used (hint: more overhead, slower runtime).

The overhead if anything is fractions of a second (or millisecond), and pretty much negligible in a learning situation (and most practical implementations). That's like the using pre-increments instead of post-increments to avoid the copy.

I personally prefer using endl for small blocks of output. /n is great for breaking up huge blocks of output. Though I suppose it really comes to readability for the programmer.

  • 0
The overhead if anything is fractions of a second (or millisecond), and pretty much negligible in a learning situation (and most practical implementations). That's like the using pre-increments instead of post-increments to avoid the copy.

I personally prefer using endl for small blocks of output. /n is great for breaking up huge blocks of output. Though I suppose it really comes to readability for the programmer.

A language designed to be runtime efficient should also be taught in that manner. Calling a method every time you want a line break is overhead, plain and simple. Is it negligible? More often than not, but that doesn't negate that peeps new to C++ should learn what it is they're doing with endl. In some applications, milliseconds can matter.

  • 0

Good point, I didn't know that. I've investigated on this issue and came upon this article by Scott Meyers. He notes:

One reviewer (Eric Nagler) brought up a more important problem.

He wrote:

The user is going to have the buffer flushed, like it or not,

because of the fact that the ios::unitbuf bit is turned on

by default. So using ?\n? vs. endl is a moot point. If the

buffer really should not be flushed, then this bit needs to be

turned off.

Had I been living under a rock? I?d never heard of ios::unitbuf.

So I turned to Steve Teale?s book on IOstreams [cite it here]. There I

found that if unitbuf is set, an ostream?s buffer is automatically

flushed after each write operation, just as Eric had written. (This is

illustrative of why I am firmly committed to having reviewers.)

However, Teale?s book also said that the default setting for unitbuf

is implementation-dependent, so I was hoping I could still squeak

the endl Item in. After all, if many implementations defaulted

unitbuf to 0, my advice would still be valid much of the time, and

Teale?s book implied that it was common to default unitbuf to 0.

Just to be on the safe side, I looked unitbuf up in the April 1995

draft ANSI/ISO C++ standard document [cite it here]. I was unable

to determine the default setting for the bit, but while wandering

around the iostream portion of the standard, I noticed something

that took me by surprise: cout is supposed to be unbuffered. You

may not find this to be a great shock, but traditionally cout has been

a buffered ostream. For unbuffered output, the traditional stream

of choice has been cerr. It seems the ANSI/ISO committee decided

to part with tradition.

My guess is that cout is still buffered under many (if not most)

implementations, but the standard will be the standard, so in the future,

cout will not be buffered. Yet most of the places where I observe

endl being used gratuitously, the destination stream is cout.

Under those conditions, the call to endl is still unnecessary (the

stream is already being flushed after each write), but it?s no longer

defeating the buffering system as I said it was in my would-be Item

above. Once implementations come into conformance with the standard,

then, my explanation would have been misleading.

  • 0

Don't includes usually look something like this:

#include &lt;iostream.h&gt;

Notice the '.h', or did they now make C++ not need the '.h' in the inclusion?

Also, another good place to go for advice is Experts Exchange:

http://www.experts-exchange.com/

It's where I go when I am stuck on something; and if you go the EE route...don't make your questions appear like school assignments.

  • 0
Good point, I didn't know that. I've investigated on this issue and came upon this article by Scott Meyers. He notes:

Good article. There is a lot of misleading information about this bit of trivia. There it is then! :) Apparently, it may still be implementation specific, but the standard is that cout is unbuffered. It's pretty sad that came out in '95 and there is still confusion about it. I know I've read recent articles that pooh-pooh the use of endl, which is what I've based my assertions on.

Edited by azcodemonkey
  • 0
Don't includes usually look something like this:

#include &lt;iostream.h&gt;

Notice the '.h', or did they now make C++ not need the '.h' in the inclusion?

Also, another good place to go for advice is Experts Exchange:

http://www.experts-exchange.com/

It's where I go when I am stuck on something; and if you go the EE route...don't make your questions appear like school assignments.

Why the hell would anybody use Experts Exchange and pay the fee when just about everything on there can be found on similar free websites like Daniweb?

  • 0
Why the hell would anybody use Experts Exchange and pay the fee when just about everything on there can be found on similar free websites like Daniweb?

I am one of their original users, have had an account since late '96...so I don't have to pay to use it; just have to pay if I want to get more points to use to ask questions.

  • 0

Thanks Dr_Asik and to everyone else who gave me pointers. I have a long way to go. Man I have been gone for a few days and this place starts hopin. :)

I have another question. When you are prompting the user for input. My code does get the answer for the sides of a box. But what I want it to do is ok using Volume=abc. I want the user to only be able to type in the apropriate material to get the answer. I can type in any letter to get the answer I don't want it to do that. I want there to be three prompts for information then the answer.

I have tried these combinations and it still doesn't work:

// Get input from user

cout << "Enter the length of the sides of the box: ";

cout << "Enter the length of the sides of the box: ";

cout << "Enter the length of the sides of the box: ";

cin >> length;

*****************************************

// Get input from user

cout << "Enter the length of the sides of the box: ";

cin >> length;

cout << "Enter the length of the sides of the box: ";

cin >> length;

cout << "Enter the length of the sides of the box: ";

cin >> length;

*******************************************

// Get input from user

cout << "Enter the length of the sides of the box: ";

cin >> length;

// Calculate Volume = abc, meaning abc are the lengths of the boxes sides

Volume = length*length*length;

// Output the Volume

cout << "The Volume is " << Volume << '\n';

// Get input from user

cout << "Enter the length of the sides of the box: ";

cin >> length;

// Calculate Volume = abc, meaning abc are the lengths of the boxes sides

Volume = length*length*length;

// Output the Volume

cout << "The Volume is " << Volume << '\n';

// Get input from user

cout << "Enter the length of the sides of the box: ";

cin >> length;

// Calculate Volume = abc, meaning abc are the lengths of the boxes sides

Volume = length*length*length;

// Output the Volume

cout << "The Volume is " << Volume << '\n';

************************************************************

Here is the code:

// volbox.cpp

// Calculating the volume of a box using the formula Volume = abc

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int Volume, length;

// Get input from user

cout << "Enter the length of the sides of the box a, b, and c: ";

cin >> length;

// Calculate Volume = abc, meaning abc are the lengths of the boxes sides

Volume = length*length*length;

// Output the Volume

cout << "The Volume is " << Volume << '\n';

system ("PAUSE");

return 0;

}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.