• 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 finally admits its default Windows 11 25H2, 24H2 action broke key legacy component by Sayan Sen Microsoft last week released Windows 11 KB5094126 and KB5093998 as the latest Patch Tuesday updates. Following that the company also published the accompanying dynamic updates under KB5094149, KB5095971, and KB5094156. So far the company has acknowledged two known issues that have popped up after the release which include bugged-out Office apps as well as the Recycle Bin; though there could be more at play too. Speaking of bugs and issues, Microsoft seems to have finally acknowledged a problem that probably has been around for close to a year. That's because back in July of 2025 the company made a default change to the latest Windows 11 versions, wherein it switched to JScript9Legacy on Windows 11 24H2 and later releases. Hence following the release of version 25H2 in October 2025, JScript9Legacy also remained default-enabled. As a result there has been a compatibility issue ever since then. For those wondering, by switching to JScript9Legacy Microsoft intended to improve the security of modern Windows PCs by reducing vulnerabilities tied to legacy scripting like cross-site scripting (XSS), among others. XSS exploits can allow cyber-attackers to attach malicious code onto legitimate websites and use them to execute the code when a potential victim loads such a website. Hence the new JScript9Legacy engine enforced stricter execution policies and improved object handling, which should help mitigate such attacks. Microsoft today has published a new support article detailing the problem. Neowin spotted it while browsing. The company says that JScript global definitions and execution context may fail to persist across scripts, potentially breaking older dependent apps and web-based components that relied on this legacy behavior. In the article Microsoft has confirmed that the issue stems from its move away from the older jscript9.dll engine in favor of jscript9legacy.dll. As mentioned above, while the newer engine was designed to address vulnerabilities and strengthen security it also changes how JScript handles execution context. As a result functions and definitions loaded by one script could no longer remain available to subsequent scripts once execution ended. The company notes that some applications worked correctly on earlier Windows versions because the older JScript engine automatically retained global definitions and execution state between scripts. Under the newer model though that behavior is disabled by default causing certain legacy workloads and polyfill-dependent scripts to fail. Microsoft says it addressed the problem via the KB5077241 update though the fix had not been enabled automatically in the following updates. As such admins must explicitly turn on persistent JScript execution context using a Registry setting that the tech giant shared today. The configuration can be applied to individual processes or system-wide through the FEATURE_ENABLE_PERSISTENCE registry key. The steps have been outlined below: Run the following command to create the feature control registry key: reg add "HKLM\Software\Policies\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ENABLE_PERSISTENCE" Under this key, create a new DWORD (32-bit) value. Configure the value as follows: To enable persistence for specific processes only: Set the value to 1 for each target process name. To enable persistence for all processes: Add * as the key name and set its value to 1. You can find the official support article here on Microsoft's website.
    • The possibility that milk gathers back into a glass implies that gravity can be 'reversed'.
    • VidCoder 12.20 by Razvan Serea  VidCoder is a DVD/Blu-ray ripping and video transcoding application for Windows. It uses HandBrake as its encoding engine. Calling directly into the HandBrake library gives it a more rich UI than the official HandBrake Windows GUI. VidCoder can rip DVDs but does not defeat the CSS encryption found in most commercial DVDs. You’ll need the NET 8 Desktop Runtime. If you don’t have it, VidCoder will prompt you to download and install it. The Portable version is self-contained and does not require any .NET Runtime to be installed. You do not need to install HandBrake for VidCoder to work. Feature list: Multi-threaded MP4, MKV containers Completely integrated encoding pipeline: everything is in one process and no huge intermediate temporary files H.264, H.265, MPEG-4, MPEG-2, VP8, Theora video Hardware-accelerated encoding with AMD VCE, Nvidia NVENC and Intel QuickSync AAC, MP3, Vorbis, AC3, FLAC audio encoding and AAC/AC3/MP3/DTS/DTS-HD passthrough Target bitrate, size or quality for video 2-pass encoding Decomb, detelecine, deinterlace, rotate, reflect, chroma smooth, colorspace filters Powerful batch encoding with simultaneous encodes Customizable Pickers to automatically pick audio and subtitle tracks, destination, titles and more Instant source previews Creates small encoded preview clips Pause, resume encoding VidCoder 12.20 changes: Updated HandBrake core to 1.11.2. Download: VidCoder 12.20 | 47.0 MB (Open Source) Download: Portable VidCoder 12.19 | 89.3 MB Link: VidCoder Home Page | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Jordan Smith earned a badge
      Week One Done
    • Reacting Well
      BizSAR earned a badge
      Reacting Well
    • First Post
      AndreaB earned a badge
      First Post
    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      590
    2. 2
      +Edouard
      185
    3. 3
      PsYcHoKiLLa
      76
    4. 4
      Michael Scrip
      73
    5. 5
      Steven P.
      66
  • Tell a friend

    Love Neowin? Tell a friend!