Programming Error C++

Problem: This program will create a number translation class which will be used for writing checks. Design a class called Numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number. For example, the number 713 would be translated into the string seven hundred thirteen, and 8203 would be translated into eight thousand two hundred three.

The class should have a single integer member variable.
int number; The class should have a constructor that accepts a nonnegative integer and uses it to initialize the Numbers object. It should have a member function print() that prints the English description of the Numbers object.

[b]I have two errors: Can someone please run my code and help!
1. Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "num" is undefined Numbers f:\Computer Science 2\Numbers\Numbers.h 17

2.Severity Code Description Project File Line Suppression State
Error C2065 'num': undeclared identifier Numbers f:\computer science 2\numbers\numbers.h 17


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <math.h>
#ifndef NUMBERS_H
#define NUMBERS_H


using namespace std;

class Numbers
{
private:
	int number;
public:
	Numbers(int x)
	{
		num = x;
	}
	void print();

};
void Numbers::print()
{
	int n;
	int num;


	string lessThanTwenty[20] = { " ", "One", "Two", "Three", "Four", "Five" "Six", "Seven", "Eight",
		"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };

	string tens[] = { "zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty" };

	string hundreds = { " ", "Hundred" };

	string thousands = { " ", "Thousand" };

	if (num < 0)
		cout << "Number is a negative number. Please enter a number between 0-9999.";
	num = abs(num);
	n = num / 1000;

	if (n>0)
		cout << lessThanTwenty[n] << thousands;
	num %= 1000;
	n = num / 100;

	if (n > 0)
		cout << lessThanTwenty[n] << hundreds;
	num %= 100;
	if (num >= 20)
	{
		n = num / 10;
		if (n > 0)
			cout << tens[n] << " ";
	}
	else if (num >= 10)
	{
		cout << lessThanTwenty[num] << " ";

		return;
	}
	num %= 10;
	if (num > 0)
		cout << lessThanTwenty[num];
	cout << " ";
};
#endif  


Number.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <conio.h>
#include <iostream>
#include <string>
#include "Numbers.h"

using namespace std;

int main()
{
	int n;

	cout << "please enter a number between  and 9999";
	cin >> n;

	while (n != 0)
	{
		cout << "the number is:" << n << " ";
		Numbers num(n);

		cout << "enter a number between 0 and  9999";
		cin >> n;

	}// end of while loop
	_getch();
	return 0;

}


Last edited on
> 1. identifier "num" is undefined
> 2.'num': undeclared identifier
¿line numbers?
Hello ne555 both the errors are on Line 17 and when I try fix it they move to line 35.
hello guys. I am pretty new but I see a couple of things I think. like "int number" under the private access modifier, it has no use what so ever that I can see. and at any rate its private so I thought no other function outside of that class could use it without a set/get function. I changed it to "int num; and now it worked without error. but I dont think its working right still. Please correct me if I am wrong.
I am using C::B and the ABS() function uses the "stdlib.h" library and needed to be included.
Thanks ahead of time for any feedback.
Also, I may be wrong, but when it is running it bypasses the print function. as in there is no call for it or obj created.
Almost like the math is wrong.
Last edited on
the errors are on Line 17
num = x; -> number = x;

I try fix it they move to line 35.
The error does not move. hundreds and thousands are not arrays, so don't initialize them as such

->
1
2
3
	string hundreds = "Hundred";

	string thousands = "Thousand";
Can someone run the code and please help me figure this out?
Last edited on
What problems are you having, other than the ones people have already given you solutions for?
Numbers.CPP

1. you do not have the number 0 in your first cout. would be nice.
2. Everything is all one the same line almost. I changed mine to be a bit nicer.
3. There is no call for the print() function. you have it defined, you have it prototyped, you are not calling it to run in "int main()". Which causes an indefinate loop of only inputing a number.


Numbers.h

1. No comma between the "five" and "six" elements of the lessThanTwenty[20] array.
2. num = x should read number = x
3. I use C::B and it calls for "stdlib.h" to use the abs() function.
4. You do not have "hundred" and "thousand" set up as arrays and should not be initialized as one.
5. Then you need to "SET" the "number" variable, then "GET" the variable for the print function.

I have this program compiled and running 100%. If you would like for me to post the code I can. Keep in mind although it follows your rules on setup and programming. I am new to coding.
I have some fixes now I still get this one error and I've tried everything:

1.Severity Code Description Project File Line Suppression State
Error C4700 uninitialized local variable 'number' used Number c:\users\freaktstud21\desktop\number\numbers.h 37


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Numbers.H

#include <iostream>
#include <string>
#ifndef NUMBERS_H
#define NUMBERS_H


using namespace std;

class Numbers
{
private:
	int number;
public:
	Numbers(int x)
	{
		number = x;
	}
	void print();

};
void Numbers::print()
{
	
	int number;


	string lessThanTwenty[20] = { "zero ", "One", "Two", "Three", "Four", "Five" "Six", "Seven", "Eight",
		"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };

	string tens[] = { "zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty" };

	string hundreds = {  "Hundred" };

	string thousands = { "Thousand" };

	if (number < 0)
		cout << "Number is a negative number. Please enter a number between 0-9999.";
	number = abs(number);
	number = number / 1000;

	if (number>0)
		cout << lessThanTwenty[number] << thousands;
	number %= 1000;
	number = number / 100;

	if (number > 0)
		cout << lessThanTwenty[number] << hundreds;
	number %= 100;
	if (number >= 20)
	{
		number = number / 10;
		if (number > 0)
			cout << tens[number] << " ";
	}
	else if (number >= 10)
	{
		cout << lessThanTwenty[number] << " ";

		return;
	}
	number %= 10;
	if (number > 0)
		cout << lessThanTwenty[number];
	cout << " ";
}//End of class

#endif  


Numbers.Cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <conio.h>
#include <iostream>
#include <string>
#include "Numbers.h"

using namespace std;

int main()
{
	int number;

	cout << "please enter a number between  and 9999";
	cin >> number;

	while (number != 0)
	{
		cout << "the number is:" << number << " ";
		Numbers number(number);

		cout << "enter a number between 0 and  9999";
		cout << " number ";

	}// end of while loop
	
	return 0;

}




(NUMBERS.CPP)
1. you do not have the number 0 in your first cout. would be nice.
2. Everything is all one the same line almost. I changed mine to be a bit nicer.
3. There is no call for the print() function. you have it defined, you have it prototyped, you are not calling it to run in "int main()". Which causes an indefinate loop of only inputing a number.
4. you can remove the _getch(), and the "#include <conio.h> " you dont need them.


(NUMBERS.H)
1. No comma between the "five" and "six" elements of the lessThanTwenty[20] array.
2. num = x should read number = x
3. I use C::B and it calls for "stdlib.h" to use the abs() function.
4. You do not have "hundred" and "thousand" set up as arrays and should not be initialized as one.
5. Then you need to "SET" the "number" variable, then "GET" the variable for the print function.

I have this program compiled and running 100%. I can post it for you if needed.


Other than that, I am not sure what you are looking for.
Last edited on
1.Severity Code Description Project File Line Suppression State
Error C4700 uninitialized local variable 'number' used Number c:\users\freaktstud21\desktop\number\numbers.h 37


Look at this snippet:

1
2
3
4
void Numbers::print()
{

	int number;


Do you realize that this is a variable local to this function, not your class member variable?

By the way in main() you're trying to use the same variable name for multiple variables, that won't work. You can only have one variable with the same name at a time.



Last edited on
Topic archived. No new replies allowed.