• 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

    • I'd say the first one failed to be as popular as Apple anticipated, but the easy adjustment here is to make fewer of them next time around. It would only be a "flop" if it isn't possible for Apple to recover the design and factory tooling costs given the number of units sold, which I doubt would be the case. It isn't like no one bought them; it just failed to become the new hot phone of the year.
    • You're right that it does not follow the plain meaning of the word, but in this context, it is a legal term defined in 49 U.S.C. Kind of how "wire fraud" laws apply even if a physical wire was not used. Given that it is codified in law, and it isn't just automotive journalists that don't understand evolving technology, I highly doubt congress would change a well understood term just because technology makes the term slightly less actuate.
    • This is exactly why I keep saying we are not ready for human free self-driving. These little "bugs" are may seem like random one-offs. There was also the Waymo that drove between police with drawn weapons and the suspect they were pointing them at. From a software perspective it is easy to understand how those extremely rare situations may not have been programed for, but that is the point. If AI needs to be told to watch out for every possible contingency, then it can never be successful. There will always be the possibility of a first encounter that the AI needs to understand to avoid.
    • TeraCopy 4.0 Final by Razvan Serea TeraCopy is a compact program designed to copy and move files at the maximum possible speed, also providing you with a lot of features. Copy files faster. TeraCopy uses dynamically adjusted buffers to reduce seek times. Asynchronous copy speeds up file transfer between two physical hard drives. Pause and resume transfers. Pause copy process at any time to free up system resources and continue with a single click. Error recovery. In case of copy error, TeraCopy will try several times and in the worse case just skips the file, not terminating the entire transfer. Interactive file list. TeraCopy shows failed file transfers and lets you fix the problem and recopy only problem files. Shell integration. TeraCopy can completely replace Explorer copy and move functions, allowing you work with files as usual. TeraCopy is free for non-commercial use only. For commercial use you need to buy a license. The paid version of the program includes the following features: Copy/move to your favorite folders. Save reports as HTML and CSV files. Select files with the same extension/folder. Remove the selected files from the copy queue. Download: TeraCopy 4.0 | 14.6MB (Freeware, paid upgrade available) View: TeraCopy Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • 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
      586
    2. 2
      +Edouard
      187
    3. 3
      Michael Scrip
      74
    4. 4
      PsYcHoKiLLa
      72
    5. 5
      neufuse
      69
  • Tell a friend

    Love Neowin? Tell a friend!