• 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.
  • Posts

    • Microsoft Edge 149.0.4022.80 by Razvan Serea Microsoft Edge is a super fast and secure web browser from Microsoft. It works on almost any device, including PCs, iPhones and Androids. It keeps you safe online, protects your privacy, and lets you browse the web quickly. You can even use it on all your devices and keep your browsing history and favorites synced up. Built on the same technology as Chrome, Microsoft Edge has additional built-in features like Startup boost and Sleeping tabs, which boost your browsing experience with world class performance and speed that are optimized to work best with Windows. Microsoft Edge security and privacy features such as Microsoft Defender SmartScreen, Password Monitor, InPrivate search, and Kids Mode help keep you and your loved ones protected and secure online. Microsoft Edge has features to keep both you and your family protected. Enable content filters and access activity reports with your Microsoft Family Safety account and experience a kid-friendly web with Kids Mode. The new Microsoft Edge is now compatible with your favorite extensions, so it’s easy to personalize your browsing experience. Microsoft Edge 149.0.4022.80 changelog: Fixes Fixed an issue that prevented QR code generation from working. Feature updates Intune MAM Protected Downloads. The protected downloads feature for Intune MAM will now save downloaded files to the Documents > Microsoft Edge > Downloads folder in OneDrive. Extensions monitoring in the Edge management service. The Microsoft Edge management service now allows admins to gain visibility into extensions installed across their managed users. From the extensions monitoring page, admins can see which extensions have been installed as well as manage user requests for blocked extensions. For more information, see Microsoft Edge Extensions Monitoring. Validate Edge builds early with enterprise preview. Enterprise preview provides a simpler way for admins to flight pre-release Edge builds to their users. To reduce friction and bolster usage, users will receive pre-release builds directly inside of their Stable Edge application. Admins can allow users to easily opt-out of the preview experience, using built-in rollback to switch between their pre-release and stable channels with ease. Microsoft 365 admin center users can configure the feature, view their flighting population, and receive personalized recommendations all in one place. For more information, see Get started with Enterprise Preview in Microsoft Edge. Download: Microsoft Edge (64-bit) | 193.0 MB (Freeware) Download: Microsoft Edge (32-bit) | 170.0 MB Download: Microsoft Edge (ARM64) | 188.0 MB View: Microsoft Edge Website | Release History Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • The machines are starting to fight back any way they can.
    • No news articles about the Arch Linux repo being majorly infected with malware?!?
    • Waymo recalls self-driving software after cars enter closed freeway work zones by Paul Hill Waymo, the self-driving car maker owned by Alphabet – the parent company of Google –, has recalled some of its fifth-generation Automated Driving Systems (ADS). It did so after some of its cars drove through closed construction zones. According to the National Highway Traffic Safety Administration (NHTSA), the affected vehicles were capable of driving through a closed freeway construction zone and continuing to drive at speed. The listing on the NHTSA website says that Waymo is currently developing a solution to fix this issue, but in the meantime, freeway driving is being restricted. Waymo will update its ADS software so that vehicles can detect when they can avoid entering construction zones. According to the Safety Recall Report, on April 20, 2026, Waymo’s Field Safety Committee began meetings reviewing an event from April 11, 2026, and five events from April 19, 2026, where Waymo’s autonomous vehicles didn’t recognize and drove past ramp closure signs into the pre-planned freeway construction zones. This took place in Phoenix, Arizona. Separately, on May 18, 2026, seven Waymo vehicles entered freeway lanes with active construction in the San Francisco Bay Area by driving between cones that were placed to show the lane was closed. On the back of both of these events, Waymo restricted freeway driving until it could address the issue. In June, Waymo’s Safety Board reviewed the issue and additional information related to ADS performances around construction zones; then, as a result, it decided to conduct a recall. This development is not good for Waymo as it adds to a growing list of technical hiccups its cars have experienced. Ultimately, it will lead to more scrutiny from lawmakers around the world who will be more cautious about letting autonomous vehicles on their roads without tighter regulation. For readers in areas where Waymo operates, does this news make you more wary about stepping into one of these vehicles?
    • I'm still on Windows 10 22H2 because I didn't want to deal with all the issues in Windows 11, so I waited almost a week before installing the latest Patch Tuesday update (KB5094127), I went ahead and did it, and it was a huge mistake—ever since then, my File Explorer has seen a performance drop of about 30% when transferring large files... Once again, Microsoft has outdone itself! This update cannot be uninstalled, either through the Control Panel (via Settings) or by accessing Advanced Startup Options. The only possible alternative would be to use system restore points, but I’d have to reinstall all app and driver updates (and there’s no guarantee it would work). Or there’s the “nuclear option” of a in-place repair without losing files or apps, but even then, all my customizations would be lost! Microsoft just can’t help but mess everything up! Way to go, Microsoft! But I still don’t want your c****y Windows 11!
  • Recent Achievements

    • Week One Done
      Eurosoft10 earned a badge
      Week One Done
    • One Month Later
      Eurosoft10 earned a badge
      One Month Later
    • One Year In
      Skeet Campbell earned a badge
      One Year In
    • One Month Later
      Sharbel earned a badge
      One Month Later
    • First Post
      BizSAR earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      599
    2. 2
      +Edouard
      190
    3. 3
      PsYcHoKiLLa
      79
    4. 4
      Michael Scrip
      77
    5. 5
      Steven P.
      69
  • Tell a friend

    Love Neowin? Tell a friend!