Sign in to follow this  
malcolmj

help for a beginner

Recommended Posts

Hi there,

I'm new to Gamedev.net and c++ programming in general, which is why I have this incredibly trivial problem that I'm trying to solve with my C++ code.

Anyone know what's the best way to get my program to write "please enter number " one, two, three, four, five. So that it keeps changing up to the fifth time? I tried a if, else if loop which seemed to go horribly wrong?

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]<iostream>
[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]<string>

[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]using[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]namespace[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] std;

[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] main(){
[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][/color][/size][/font][/color][/size][/font][/color][/size][/font]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] array1[5];
[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] n = 0;

[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]for[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]([/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] n=0; n<5; n++){

cout << [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"Please enter a number: "[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] x;
cin >> x;
array1[n] = x;

}

cout << [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"The five numbers you entered are: "[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
cout << array1[0] << endl;
cout << array1[1] << endl;
cout << array1[2] << endl;
cout << array1[3] << endl;
cout << array1[4] << endl;

system ([/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"pause"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]);
[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] 0;

}

[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
Your loop goes from i = 0 to i < 5, which means i becomes 0, 1, 2, 3, 4. You can output (i + 1) in your std::cout statement.

Also, you can do your final output in a loop too.

Share this post


Link to post
Share on other sites
Thanks mate,

I managed to tidy up my little code a bit:

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]<iostream>
[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]#include[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]<string>

[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"][/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]using[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]namespace[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] std;

[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] main(){



[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] array1[5];
[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] i = 0;
string listnumber;

[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]for[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](i=0; i<5; i++){

[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](i == 0){
listnumber = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"first"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
} [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]else[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](i == 1){
listnumber = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"second"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
} [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]else[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](i == 2){
listnumber = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"third"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
} [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]else[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](i == 3){
listnumber = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"forth"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
} [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]else[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](i == 4){
listnumber = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"fifth"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
}

cout << [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"Please enter your "[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] << listnumber << [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]" number: "[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] x;
cin >> x;
array1[i] = x;

}

cout << [/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"The five numbers you entered are: "[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];
i = 0;
[/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]for[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](i=0; i<5; i++){
cout << array1[i] << endl;
}

system ([/size][/font][/size][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]"pause"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]);[/size][/font][/size][/font]

[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] 0;

}

[/size][/font][/size][/font]

Share this post


Link to post
Share on other sites
[quote name='malcolmj' timestamp='1298456837' post='4777901']
I managed to tidy up my little code a bit:
[/quote]
If you want to tidy it up a little more, consider making a function that takes an integer and returns the ordinal number ( See the wikipedia article on Ordinal Number(Linguistics) ) as a std::string. I suggest something like:

[code]std::string int_to_ordinal(int n) {
...
}

cout << "Please enter your " << int_to_ordinal(i) << " number: ";[/code]
This is a good thing to do because it seperates the responsibilities of your code, makes it easier to read what the loop is doing, and allows for greater code reuse. If you want to output the ordinal number. I'm a little iffy on whether int_to_ordinal is a good name for the function, but the concept is the important thing. Next, when you have the user enter the number you create a temporary x and then immediately assign x to array1[i]. You can skip that step and just assign to the array[i] directly. Another thing, you can create the int variable i in the for loop directly instead of using it outside the loop. This is generally preferred in C++ other the way you are doing it unless you need to use that variable again outside the loop. Combining all of this and a few other things together gives you:

[code]
#include <iostream>
#include <string>

using namespace std;

string int_to_ordinal(int i) {
// you can rewrite this anyway you want in the future. perhaps use a switch
// statement or some more complicated algorithm that would work for any
// number, not just 1-5
if(i == 1){
return "first";
} else if(i == 2){
return "second";
} else if(i == 3){
return "third";
} else if(i == 4){
return "fourth";
} else if(i == 5){
return "fifth";
}
return ""
}

int main(){
int array1[5];

for(int i = 0; i < 5; i++){
cout << "Please enter your " << int_to_ordinal(i+1) << " number: ";
cin >> array1[i];
}
cout << "The five numbers you entered are: ";
for(int i = 0; i < 5; i++){
cout << array1[i] << endl;
}

system ("pause");
// return 0; is not necessary at the end of main
}

[/code]

Share this post


Link to post
Share on other sites
You can use an array of the ordinal names too:
[code]
const std::string ordinals[] = { "first", "second", "third", /* ... */ };

std::string int_to_ordinal(int i)
{
return ordinals[i - 1];
}
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this