Jump to content
  • Advertisement
Sign in to follow this  
malcolmj

help for a beginner

This topic is 2699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<iostream>
[/font][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<string>

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

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

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

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

}

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

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

}

[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
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"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<iostream>
[/font][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<string>

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

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



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

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

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

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

}

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

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

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

}

[/font][/font]

Share this post


Link to post
Share on other sites

I managed to tidy up my little code a bit:

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:

std::string int_to_ordinal(int n) {
...
}

cout << "Please enter your " << int_to_ordinal(i) << " number: ";

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. You can skip that step and just assign to the array 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:


#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;
}
cout << "The five numbers you entered are: ";
for(int i = 0; i < 5; i++){
cout << array1 << endl;
}

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

Share this post


Link to post
Share on other sites
You can use an array of the ordinal names too:

const std::string ordinals[] = { "first", "second", "third", /* ... */ };

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!