Loops

This is code that is going into a function that when put into the program will add Roman Numerals

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
    cin >> RomanNum1;
    while (!cin.eof())
	{

        if (RomanNum1 == One)
		{
            ICount++;
        }
        else if (RomanNum1 == Five)
		{
            VCount++;
        }
        else if (RomanNum1 == Ten)
		{
            XCount++;
        }
        else if (RomanNum1 == Fifty)
		{
            LCount++;
        }
        else if (RomanNum1 == OneHundred)
		{
            CCount++;
        }
        else if (RomanNum1 == FiveHundred)
		{
            DCount++;
        }
        else if (RomanNum1 == OneThousand)
		{
            MCount++;
        }
        else {
            return 0;
        }

        cin >> RomanNum1;
	
	}


All the used variables are initialized. For some reason it doesn't even let me input anything it simply ends when I debug. Any ideas as to why?
This is not enough of your program for us to see the problem.

By the way, the proper way to loop on input is like this:
1
2
3
4
while(cin >> RomanNum1)
{
    //...
}
loop doesn't make sense. eof means end of file and you are not inputting from a file.
The term "end of file" is a hold-over from C, where all streams were dealt with via FILE pointers, even if they did not refer to files. Really, the correct term is "end of stream". The loop will work as is, it is just a bad way to do it.
This is my full code, for whatever reason the programs simply ends without allowing input
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <string>
#include <iostream>     
#include <iomanip>     
using namespace std;


const char One = 'I';
const char Five = 'V';
const char Ten = 'X';
const char Fifty = 'L';
const char OneHundred = 'C';
const char FiveHundred = 'D';
const char OneThousand = 'M';

const char Plus = '+';
const char Minus = '-';
const char Times = '*';
const char Divide = '/';

const int ValueOfOne = 1;
const int ValueOfFive = 5;
const int ValueOfTen = 10;
const int ValueOfFifty = 50;
const int ValueOfOneHundred = 100;
const int ValueOfFiveHundred = 500;
const int ValueOfOneThousand = 1000;

int main ()
{
    char RomanNum1, RomanNum2;
    char Operation;
    int Answer; 
    string Response;

    int ICount = 0;
    int VCount = 0;
    int XCount = 0;
    int LCount = 0;
    int CCount = 0;
    int DCount = 0;
    int MCount = 0;

    int Sum = 0;


    cin >> RomanNum1;
    while (cin >> RomanNum1)
	{

        if (RomanNum1 == One)
		{
            ICount++;
        }
        else if (RomanNum1 == Five)
		{
            VCount++;
        }
        else if (RomanNum1 == Ten)
		{
            XCount++;
        }
        else if (RomanNum1 == Fifty)
		{
            LCount++;
        }
        else if (RomanNum1 == OneHundred)
		{
            CCount++;
        }
        else if (RomanNum1 == FiveHundred)
		{
            DCount++;
        }
        else if (RomanNum1 == OneThousand)
		{
            MCount++;
        }
        else
		{
            return 0;
        }

        cin >> RomanNum1;
	
	}

    cin >> RomanNum2;
    while (cin >> RomanNum2)
	{

        if (RomanNum2 == One)
		{
            ICount++;
        }
        else if (RomanNum2 == Five)
		{
            VCount++;
        }
        else if (RomanNum2 == Ten)
		{
            XCount++;
        }
        else if (RomanNum2 == Fifty)
		{
            LCount++;
        }
        else if (RomanNum2 == OneHundred)
		{
            CCount++;
        }
        else if (RomanNum2 == FiveHundred)
		{
            DCount++;
        }
        else if (RomanNum2 == OneThousand)
		{
            MCount++;
        }
        else 
		{
            return 0;
        }
        cin >> RomanNum2;
    }

    cin >> Operation;
    if (Operation == Plus)
	{
        Answer = RomanNum1 + RomanNum2;
        Response = "sum";
    }
    else if (Operation == Minus)
	{
        Answer = RomanNum1 - RomanNum2;
        Response = "difference";
    }
    else if (Operation == Times)
	{
        Answer = RomanNum1 * RomanNum2;
        Response = "product";
    }
    else 
	{
        Answer = RomanNum1 / RomanNum2;
        Response = "quotient";
    }

    Sum = ValueOfOne * ICount 
		+ ValueOfFive * VCount 
		+ ValueOfTen * XCount 
        + ValueOfFifty * LCount 
		+ ValueOfOneHundred * CCount 
		+ ValueOfFiveHundred * DCount 
		+ ValueOfOneThousand * MCount;

    cout << "The first number is " << RomanNum1 << endl;
    cout << "The second number is " << RomanNum2 << endl;
    cout << "Arithmetic operation is " << Operation << endl;
    cout << "The " << Response << " of " << RomanNum1 << " and "
		<< RomanNum2 << " is " << Sum << endl;

}

This is my full code, for whatever reason the programs simply ends without allowing input
Last edited on
dragonslayerealize wrote:
This is my full code, for whatever reason the programs simply ends without allowing input
I don't believe you. It takes input for me.

Also, you did not follow my advice correctly - you have extra input statements. Not only that, but how do you expect the loops to end and then go to the next loop? The only way is to end the input, so what does the next loop get?
I figured out the issue with the input possibly.

However now I cant get it to read out anything...could this possibly be because it never leaves the loop?
L B wrote:
Not only that, but how do you expect the loops to end and then go to the next loop? The only way is to end the input, so what does the next loop get?
The input was being restricted by a .txt file. Simply me being sloppy.


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

	while (cin >> RomanNum1)
	{

        if (RomanNum1 == One)
		{
            ICount++;
        }
        else if (RomanNum1 == Five)
		{
            VCount++;
        }
        else if (RomanNum1 == Ten)
		{
            XCount++;
        }
        else if (RomanNum1 == Fifty)
		{
            LCount++;
        }
        else if (RomanNum1 == OneHundred)
		{
            CCount++;
        }
        else if (RomanNum1 == FiveHundred)
		{
            DCount++;
        }
        else if (RomanNum1 == OneThousand)
		{
            MCount++;
        }
     

	
	}



Is this what you mean by getting rid of inputs? I do not understand how to make it exit the loop still
Last edited on
Do you have to use constants for those integers? Just curious if it were a requirement.
dragonslayerealize wrote:
Is this what you mean by getting rid of inputs?
Yes.
dragonslayerealize wrote:
I do not understand how to make it exit the loop still
It's entirely your choice. Maybe you want to make it break from the loop when a space is entered?

Should this work? Side note the Numerals input are things like XVIII

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
while (cin >> RomanNum1)
	{

        if (RomanNum1 == One)
		{
            ICount++;
        }
        else if (RomanNum1 == Five)
		{
            VCount++;
        }
        else if (RomanNum1 == Ten)
		{
            XCount++;
        }
        else if (RomanNum1 == Fifty)
		{
            LCount++;
        }
        else if (RomanNum1 == OneHundred)
		{
            CCount++;
        }
        else if (RomanNum1 == FiveHundred)
		{
            DCount++;
        }
        else if (RomanNum1 == OneThousand)
		{
            MCount++;
        }
		else
		{
		break;
		}
	}

Last edited on
Now any non-roman-numeral character will break the loop. If that's what you want, then by all means it's fine.
Topic archived. No new replies allowed.