Triangle help solve problem

User enters one number with word example: three. Possible entries are numbers from one to nine. Write out words(numbers) in seperated lines. In every seperate line add one more word(number). Seperate numbers with comma.


example:
User enters five.
Output should look like:
five
five, five
five, five, five
five, five, five, five
five, five, five, five, five

User enters three:
three
three, three
three, three, three

Can you help me solve this problem. I've got and idea but i don't know how to write it down.

For instance:
I would count lenght of the word in one word(number). Possible lenght in letters would be 3, 4 and 5. Example: one has three letters. Four has 4 letters. Seven has 5 letters. With this i can eliminate all other entries. But i don't how would i compare later entry with the possible numbers.

Any suggestions?


Use a lookup table:
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 <iostream>
#include <string>

int main(int argc, char* argv[])
{
    const int NUM_DIGITS = 10;
    std::string lookup[NUM_DIGITS] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    //what does this buy us? Well, lookup[0] = "zero",
    //                             lookup[1] = "one", etc...


    std::string someStringFromUser("seven");

    int intVersionOfUserInput;
    for(int i = 0; i < NUM_DIGITS; i++)
    {
        if(someStringFromUser == lookup[i])
        {
            intVersionOfUserInput = i;
            break;
        }
    }

    std::cout << "I got the word " << someStringFromUser << " from the user and turned it into the integer "
              << intVersionOfUserInput << "! Hooray!" << std::endl;
    return 0;
}

Once you get the text from the user into an int form, it is much more useful to your program.
Can i exclude std:: because i never really use it in school? And include to the library

using namespace std;

What does the break command do?

And if i understand right basically i is integer who goes through array and then that if statement compares with numbers?

Sorry for bad english
using namespace std; is not a library. It just tells the compiler that you're going to be using some symbols defined in the std namespace. Things like string, cout, and endl are all things that exist in the std namespace. I didn't have to declare anything named cout, I just use cout. If you say using namespace std;, you don't have to put std:: in front of string, cout, and endl.

break; makes the program jump out of the current loop. If I find a match, I don't want to keep looking, so I break out of the for loop.

The loop matches a string to a lookup value. That's how I get an integer 7 from the string "seven".
What about const? You have to include it or not?

Here is how i imagined i would print out the numbers.

1
2
3
4
5
6
7
8
9
10
   int i=0;
for(;i < intVersionOfUserInput;)
    {  cout<<someStringFromUser<<", "<<endl;
       i=i+1;
       someStringFromUser=someStringFromUser+", seven";

    }


   


But somehow my programms gets std:: bad alloc or something and i can't even test how does it print out..
Last edited on
You are not using the for loop properly. You have created an infinite loop that doubles the size of a string every time it executes. Eventually, you run out of memory and your program can no longer allocate new memory. That's what bad_alloc means.

Look here for more information regarding the use of a for loop:
http://www.cplusplus.com/doc/tutorial/control/#for
i already fixed it my bad.

But i dont know what should i do that instead of ", seven" I could write something else because if i write someStringFromUser again then program just gets out of control.


1
2
3
4
5
6
7
8
9
string number;
    number=someStringFromUser;
    int i=0;
for(;i < intVersionOfUserInput;)
    {  cout<<someStringFromUser<<endl;
       someStringFromUser=someStringFromUser+", "+number;
       i=i+1;

    }


I did this and works fine. What do you think?

Now the next question is what should i do so that program will tell you to try again if you entered bad number or something else?

And do you have any ideas how could i transform Upper case letters to lower case?

example:
User enters number SeVeN and i would transform it seven so that i can still write out a triangle. if you know what i mean
Last edited on
This:
1
2
3
4
5
6
7
int i=0;
for(;i < intVersionOfUserInput;)
    {  cout<<someStringFromUser<<endl;
       someStringFromUser=someStringFromUser+number;
       i=i+1;

    }


is more eloquently expressed like this:
1
2
3
4
5
for (int i = 0; i < intVersionOfUserInput; i = i + 1) //i = i + 1 can be replaced with i++
{
    cout<<someStringFromUser<<endl;
    someStringFromUser=someStringFromUser+number;
}


The goal of your exercise is to get you to understand how to use nested loops.
1
2
3
4
5
6
7
for (int i = 0; i < intVersionOfUserInput; i++)
{
    //print someStringForUser a certain amount of times using another for loop
    //(Hint: it will be based on the current value of i)

    //print a new line
}
Last edited on
Sorry but i dont understand what u meant with the exercise...

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < intVersionOfUserInput; i++)
{
    //print someStringForUser a certain amount of times using another for loop
    //(Hint: it will be based on the current value of i)
    for(;;)
    {
     cout<<someStringForUser;
     cout<<endl;
     }


    //print a new line
}
Topic archived. No new replies allowed.