• 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

To validate input, you could define your length variable as an STL string. Then, iterate over each character in the string using the function isdigit() to verify that all characters are indeed digits. If you want to allow the user to retry if he made a mistake, your best bet is to make a while loop.

Here's my idea for a basic validation function :

#include &lt;string&gt;
#include &lt;iostream&gt;

using namespace std;

bool isValidEntry(string entry) {
	for (string::iterator it = entry.begin(); it != entry.end(); it++) {
		if (!isdigit(*it)) {
			return false;
		}
	}
	return true;
}

int main() {

	string entry;

	cout &lt;&lt; "Welcome. Enter a number.";
	cin &gt;&gt; entry;
	if (isValidEntry(entry)) {
		cout &lt;&lt; "Woohoo" &lt;&lt; endl;
	}
	else {
		cout &lt;&lt; "Noooo" &lt;&lt; endl;
	}



	system("pause");
	return 0;
}

  • 0
To validate input, you could define your length variable as an STL string. Then, iterate over each character in the string using the function isdigit() to verify that all characters are indeed digits. If you want to allow the user to retry if he made a mistake, your best bet is to make a while loop.

Here's my idea for a basic validation function :

#include &lt;string&gt;
#include &lt;iostream&gt;

using namespace std;

bool isValidEntry(string entry) {
	for (string::iterator it = entry.begin(); it != entry.end(); it++) {
		if (!isdigit(*it)) {
			return false;
		}
	}
	return true;
}

int main() {

	string entry;

	cout &lt;&lt; "Welcome. Enter a number.";
	cin &gt;&gt; entry;
	if (isValidEntry(entry)) {
		cout &lt;&lt; "Woohoo" &lt;&lt; endl;
	}
	else {
		cout &lt;&lt; "Noooo" &lt;&lt; endl;
	}



	system("pause");
	return 0;
}

I do like that but what I am looking for is when I type in the number or letter it goes ahead and calculates everything. Here is an example:

Enter SideA:

a

Enter SideB: (it won't let my type in SideB it just skips to the next line)

Enter SideC: (it won't let me type in SideC it just skips to the next line and calculates)

The Volume is 6

Here is my new code problems from above included:

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int SideA;

SideA = 1;

int SideB;

SideB = 2;

int SideC;

SideC = 3;

int volume;

// 1. Prompt for SideA

cout << "Enter SideA: " << endl;

// 2. Get SideA

cin >> SideA;

// 3. Prompt for SideB

cout << "Enter SideB: " << endl;

// 4. Get SideB

cin >> SideB;

// 5. Prompt for SideC

cout << "Enter SideC: " << endl;

// 6. Get SideC

cin >> SideC;

// 7. Calculate Volume

// Volume = SideA * SideB * SideC

int Volume = SideA * SideB * SideC;

// 8. Display Volume

cout << "The Volume is " << Volume << endl;

system ("PAUSE");

return 0;

}

  • 0

Use a c-style string and cin.getline() for input. For instance:

char inputBuffer[256];

cout << "Enter first number";

cin.getline(inputBuffer, 256);

cout << "Enter second number";

cin.getline(inputBuffer, 256);

Now, you'll run in trouble if the user exeeds 256 characters, but let's keep this simple shall we? :p

Then convert to integer using atoi (in <cstdio>).

  • 0
Use a c-style string and cin.getline() for input. For instance:

char inputBuffer[256];

cout << "Enter first number";

cin.getline(inputBuffer, 256);

cout << "Enter second number";

cin.getline(inputBuffer, 256);

Now, you'll run in trouble if the user exeeds 256 characters, but let's keep this simple shall we? :p

Then convert to integer using atoi (in <cstdio>).

I got it to work thanks. :) What exactly does the inputBuffer[256] mean?

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

int SideA;

SideA = 1;

int SideB;

SideB = 2;

int SideC;

SideC = 3;

char inputBuffer[256];

// 1. Prompt for SideA

cout << "Enter SideA";

// 2. Get SideA

cin.getline(inputBuffer, 256);

// 3. Prompt for SideB

cout << "Enter SideB";

// 4. Get SideB

cin.getline(inputBuffer, 256);

// 5. Prompt for SideC

cout << "Enter SideC";

// 6. Get SideC

cin.getline(inputBuffer, 256);

// 7. Calculate Volume

// Volume = SideA * SideB * SideC

int Volume = SideA * SideB * SideC;

// 8. Display Volume

cout << "The Volume is " << Volume << endl;

system ("PAUSE");

return 0;

}

  • 0

char inputBuffer[256] declares an array of 256 characters named "inputBuffer". It's a C-style string, as opposed to a C++-style string. Unfortunately in C++ we generally need to use both and they work very differently. That was a world of confusion for me when I began (and still is, to a more limited extent).

Edited by Dr_Asik
  • 0
char inputBuffer[256] declares an array of 256 characters named "inputBuffer". It's a C-style string, as opposed to a C++-style string. Unfortunately in C++ we generally need to use both and they work very differently. That was a world of confusion for me when I began (and still is, to a more limited extent).

That makes sense thank you :)

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

    • No registered users viewing this page.
  • Posts

    • Just saw a news report of a Waymo driving into a flooded road.
    • Password Safe 3.72.0 by Razvan Serea Password Safe is a password database utility. Like many other such products, commercial and otherwise, it stores your passwords in an encrypted file, allowing you to remember only one password (the "safe combination"), instead of all the username/password combinations that you use. Once stored, your user names and passwords are just a few clicks away. Using Password Safe you can organize your passwords using your own customizable references—for example, by user ID, category, web site, or location. You can choose to store all your passwords in a single encrypted master password list (an encrypted password database), or use multiple databases to further organize your passwords (work and home, for example). And with its intuitive interface you will be up and running in minutes. PasswordSafe was originally designed by the renowned security technologist Bruce Schneier and released as a free utility application. Password Safe 3.72.0 changelog: Fixed bugs Improved font scale handling - should resolve font size issues on high resolution displays. GH1749 In the Master Password Setup window, "Show Master Password" is no longer truncated on some displays. GH1092, SF1595 Size and position of main window is now correctly restored on scaled displays. SF1630 Keep password expiry date when both password and password expiry are changed; don't clear a non-recurring expiry when the password's changed. SF1628 Custom values can now be copied to the clipboard in read-only mode via Ctrl-C and right-click->Copy Value. New features GH1196 Dark display mode support: Password Safe now supports the system display mode, as well as setting the mode directly via Manage->Options->Display->Display Mode. This change also updates the general "look & feel" of the app to the current Windows theme. Known limitations: The Date picker and keyboard shortcut controls do not switch to dark theme The Customize Toolbar dialog does not switch to dark theme Custom Field support has been added to the more advanced features: Filters XML and Text import and export Comparison, Sync and Merge databases SF938 Custom field values may now be selected by name and copied via a "Copy Custom Field Value..." submenu in the entry context popup menu. SF936 Notes and Custom fields layout now overlap, selectable by tabs, resulting in a more compact and less cluttered layout. SF935 Autotype: Specifying '\v{name}' in the autotype text will cause the corresponding value to be autotyped. Download: PasswordSafe 64-bit | Portable 64-bit | ~20.0 MB (Open Source) Download: PasswordSafe 32-bit | Portable 32-bit View: PasswordSafe Website | Quickstart Guide | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Google DeepMind published a document on June 18, 2026, that may be the most consequential admission yet from a frontier AI lab: alignment training alone cannot guarantee that AI agents will remain under human control, so structural containment must be built before more capable models arrive.............. https://www.techtimes.com/articles/318758/20260620/google-deepmind-ai-control-roadmap-when-alignment-fails-defense-depth-takes-over.htm  
    • I've got a SoundBlasterX G6 that I use in my streaming setup. Sounds great to me and I've had zero issues with the ancient software package so far in Win11. That G6 has 7.1, Dolby, fully working SPDIF and since it's a USB device it's outside of my rig so I don't have to worry about EMF distortion. Looks like for now this is a pass for me as I think I have better hardware....
    • How do you connect 5.1 Speakers to this thing?
  • Recent Achievements

    • Dedicated
      JuvenileDelinquent earned a badge
      Dedicated
    • First Post
      DrWankel earned a badge
      First Post
    • Reacting Well
      DrWankel earned a badge
      Reacting Well
    • Week One Done
      Supreme Spray LV earned a badge
      Week One Done
    • Week One Done
      Genuinetonerink- Dubai earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      170
    3. 3
      PsYcHoKiLLa
      88
    4. 4
      Steven P.
      75
    5. 5
      Michael Scrip
      74
  • Tell a friend

    Love Neowin? Tell a friend!