If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

I want to be able to eliminate that first statement but if I only run this in the loop without the above statement the program will display nothing on the screen and proceeds to stop.

2. This code runs fine, except that if you make a mistake, it will prompt you to enter a valid number, however; it ignores your first response if the number you enter is valid and asks you to enter a valid number anyway. Once you enter it a second time, it will accept it and the program will continue on.

Also if you type in 0 on your first response, it will prompt you that it is not a valid number and ask you to try again, instead of stopping the program like it is supposed to do. On your second response the program will accept your 0 and stop the program correctly.

Here is my code:

Code:

// Write a program that displays the roman numeral equivalent
// of any decimal number between 1 and 20 that the user enters.
// The roman numerals should be stored in an array of strings
// and the decimal number that the user enters should be used to
// locate the array element holding the roman numeral equivalent.
// The program should have a loop that allows the user to continue
// entering numbers until an end sentinel of 0 is entered.
// Input validation: Do not accept scores less than 0 or greater than 20
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declare constants and variables
const int romanNum = 21; // Size of the elements in the array
// Roman numerals stored in an array
string roman[romanNum] = {"","I","II","III","IV","V","VI","VII","VIII","IX","X",
"XI","XII","XIII","XIV","XV","XVI","XVII","XIII","XIX","XX"};
const int SENTINEL = 0; // variable stays 0 during entire program
int num = 0; // variable to store user input, intialized to 0
cout << "Please enter a number between 1 and 20 (Enter 0 to stop)\n";
cin >> num;
cout << endl;
// Loop comparing user input to roman numerals in the array. If user enters 0, the program stops.
while(num != SENTINEL)
{
cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
cin >> num;
cout << endl;
// Conditional statement comparing any number entered that is greater than 0 and less than or equal to 20
if(num > 0 && num <= 20)
{
// Displays user input
cout << num << " is equivalent to " << roman[num] << endl;
}
else
{
// Input validation if a number entered is less than 0 or greater than 20
cout << "Invalid entry\n";
cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
cin >> num;
cout << endl;
}
}
system("pause");
return 0;
}

The reason it is being repeated is because the "else" statement is the input validation if the user accidentally inputs an invalid number. The compiler prompts the user to try again and I just have it giving out the same instructions as before.

Originally Posted by Paul McKenzie

This is the form that the code should take:

Code:

ask for number;
while (number is not quit)
{
if number is good
convert number to roman numeral;
else
output that number is bad;
ask for number;
}

Re: Roman Numeral Converter to Decimal Using an Array of Strings

The reason it is being repeated is because the "else" statement is the input validation if the user accidentally inputs an invalid number. The compiler prompts the user to try again and I just have it giving out the same instructions as before.

once in the program - even dealing with invalid input and keep asking the user to input until 0 is entered. Look at how the loop can be changed to just have these statements once. Also, what happens if a user inputs say an x instead of a number?

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Roman Numeral Converter to Decimal Using an Array of Strings

This is the Visual C++ forum, so given that, the compiler must come with a debugger. If not, then

1) You're in the wrong forum and
2) You need to get a better build system.

For 1) you should post in the Non-Visual C++ forum.

For 2) every compiler has to come with some sort of debugging module, else it will be difficult, if not impossible to write anything bigger than toy programs. Also, if you are a student and/or this is homework, it is considered cheating if you are asking an expert or professional programmer to debug programs instead of you doing so. So it is in your best interests to find and use a C++ build system, whether it's Visual C++, CodeBlocks using g++ or some other reputable C++ environment, that comes with a credible debugger.

But, isn't that what I am doing here?

No.

Look at my pseudo-code carefully. The version I have initially asks for input outside the loop before it's entered. Then I enter the loop if the input is good and then process it. Then at the bottom of the loop, the question is asked again, thus taking you to the top of the loop with the check for quit. Then if the number if good, process it, else, output the number is bad and input again.

Re: Roman Numeral Converter to Decimal Using an Array of Strings

This is the Visual C++ forum, so given that, the compiler must come with a debugger. If not, then

1) You're in the wrong forum and
2) You need to get a better build system.

I have Visual C++, but because the program that I use comes with its own compiler then I chose to use that. If that upsets you in any way, or any other moderator that posts here, then I apologize and will go back to using Visual, its' that simple.

As it relates to Visual, I have never used the debugger before. I am very new, and I am learning this on my own, so I had no idea about it.

Originally Posted by Paul McKenzie

Also, if you are a student and/or this is homework, it is considered cheating if you are asking an expert or professional programmer to debug programs instead of you doing so.

I am not a student, therefore your assumption that I am cheating is off base. I am learning on my own and I don't remember anywhere in my post where I specifically asked anyone reading it to debug the program for me. I simply asked a few questions...and I provided some code to go along with it as it relates to what I have learned so far.

See what happens if you remove the first prompt. You have two choices. Either use a do/while loop, which is the better approach, or initialize num so that it doesn't equal SENTINEL.

This forum is only for people using Visual C++. If you're using a different environment, you should post in the generic C++ forum, but any development environment that doesn't include a debugger is completely useless. If that's really the case, you should stop using it immediately.

Re: Roman Numeral Converter to Decimal Using an Array of Strings

Originally Posted by veryNew

I have Visual C++, but because the program that I use comes with its own compiler then I chose to use that. If that upsets you in any way,

No one is "getting upset".

This forum is called "Visual C++ forum" for a reason. The posts here are geared toward the Microsoft Visual C++ compiler, not for any other compiler. Given that, every version of Visual C++ comes with a debugger, which is why it is impossible for the Visual C++ compiler to not come with a debugger. The other issue is that you may be using a compiler that isn't ANSI compliant, causing confusion when we give you a solution, but the solution either doesn't work or doesn't compile on your compiler that you are using that isn't Visual C++.

That's why there is a Non_Visual C++ forum for such posts. In that forum, questions that do not concern the Visual C++ compiler are asked.

As it relates to Visual, I have never used the debugger before. I am very new, and I am learning this on my own, so I had no idea about it.

When you build a program, there is a "Debug" menu. If you hit F10 a few times after you get a successful build, you will see what the debugger does. You will be able to step through your code, watch variables, etc.

I am not a student, therefore your assumption that I am cheating is off base.

Re: Roman Numeral Converter to Decimal Using an Array of Strings

Originally Posted by Paul McKenzie

No one is "getting upset".

This forum is called "Visual C++ forum" for a reason. The posts here are geared toward the Microsoft Visual C++ compiler, not for any other compiler. Given that, every version of Visual C++ comes with a debugger, which is why it is impossible for the Visual C++ compiler to not come with a debugger. The other issue is that you may be using a compiler that isn't ANSI compliant, causing confusion when we give you a solution, but the solution either doesn't work or doesn't compile on your compiler that you are using that isn't Visual C++.

That's why there is a Non_Visual C++ forum for such posts. In that forum, questions that do not concern the Visual C++ compiler are asked.
When you build a program, there is a "Debug" menu. If you hit F10 a few times after you get a successful build, you will see what the debugger does. You will be able to step through your code, watch variables, etc.

You are right. It was an oversight on my part of where I posted and I should have posted on the Non-Visual C++ threads and for that I apologize.

I have never used the debugger before, but I would like to learn how. Are there any good tutorials online that explain how to use the debugger?

Re: Roman Numeral Converter to Decimal Using an Array of Strings

You are right. It was an oversight on my part of where I posted and I should have posted on the Non-Visual C++ threads and for that I apologize.

No problem.

I have never used the debugger before, but I would like to learn how. Are there any good tutorials online that explain how to use the debugger?

Basically, my advice is that you need very little if any documentation. Only when you start to really get involved with it do you need to read up on some of what the debugger does.

Build a simple application (the one you're using now is an example). Then when it's built successfully, hit F10. You will see an arrow pointing to the first exectuable line of code. Then hit F10 again -- that arrow moves to the next line. Then hit F10 again -- the arrow moves again. Then keep hitting F10 until you get to the loop. You will see the arrow jump up to the top of the loop, jump over an if() statement that returns false, etc.

Basically what you're doing is running your program in slow motion, under your control, instead of having it zoom by to completion.

While hitting F10, you should see some other windows open up, where you see the variables you have declared and their current values. Note that they change when you execute a line that changes a variable.

So that is a quick introduction. You then will learn how to set "breakpoints", where you run the program at full speed under the debugger until it hits the line where you've set the breakpoint. Then basically, things snowball from there, where you're picking up information on how to use the debugger more economically, smartly, etc. (it isn't just about hitting F10, but that is how I introduce anyone to the debugger).

Re: Roman Numeral Converter to Decimal Using an Array of Strings

To use the debugger, you need either to be logged on as an administrator or the user needs to be a member of the 'Debugger Users' group. Also, the solution needs to be configured to produce a debug build (which contains the debugging info) rather than a release build which doesn't.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.