Inputting an integer from a string in an IF statement?

I have been working on a Fibonacci Sequence algorithm, but when it comes to contextualizing the words for the console's output, I wanted it to understand the difference between plural and singular values. I decided to create separate entities that are directly affected with plurals, and named them;

n_plural
i_plural
is_plural

I am sure you can see in the code below how it is meant to function. When I type in the number "1" in the console, everything works well and it says what it should; "The first iteration in the Fibonacci Sequence is:", but when I type in something like 111, it will say something like "The firsto iterations in the Fibonacci Sequence are:" and won't even register a number in between "first" and "iterations".

Where is it getting the letter "o" from at the end of "first"? Is there a way that I can insert integer n into the n_plural IF statement so that the integer will show as a string? I am sure that the problem is created at;

else {
n_plural= n;

Also, is there a smarter way to go about making plural context without creating separate string entities in my work?

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
//fibonacci.at8569e.cpp
//g++  4.9.2

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

    long long n, i, a=0, b=1, c;
    string n_plural, i_plural, is_plural;
        
    cout << "Please enter the maximum amount of iterations: ";
    
    cin >> n;
    
    if (n==1){
        n_plural="";
        i_plural="iteration";
        is_plural="is";
    }
    else {
        n_plural= n;
        i_plural="iterations";
        is_plural="are";
    }
    if (n>=1){
        
            cout << "\n\nThe first" << n_plural << " " << i_plural << " in the Fibonacci Sequence " << is_plural<<":\n\n";
    
            cout << a;
    
            for (i=1;i<n; i++)
            {
                c=a+b;
                cout << " " << c;
                a=b;
                b=c;
            }
    }
    else {
    cout << "INVALID NUMBER";
    }
    
}


Anyway, thanks for reading, and sorry for writing so much... :D
What is line 25 supposed to do? I'm surprised it even compiles - it should be n_plural = std::to_string(n);

Also, on line 31, you forgot a space after "first"
Last edited on
As you can see, on line 25, if the user enters n as 1, the IF statement on line 19 makes the value on n_plural = "", which is essentially nothing, which means that if the user enters n as 1, it won't say "The first 1 iteration..." instead, it will ignore the "1" and just say "The first iteration..." but when a number above 1 is entered for n, such as 10, it should say "The first 10 iteration's'." Line 21 basically inserts n_plural the value on n, so that when it is called on line 31 it will present the actual number as opposed to "" when a value of one is entered.

But yeah, the missing space in line 31 is due to the fact that I can't turn the int into a string, if I could, I would slap on a " " before the int so that it will become a space and then the number.
Please read what I said: line 25 should be written as n_plural = std::to_string(n);
http://www.cplusplus.com/reference/string/to_string/
http://en.cppreference.com/w/cpp/string/basic_string/to_string
Last edited on
Thanks for editing that in.
Tselinoyarsk wrote:
Thanks for editing that in.
The edit was because I mistyped a closing brace on the code segment ;)
Topic archived. No new replies allowed.