Newbie here. [Lost]

I am lost pls help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
main()
{
    decision();
    return 0;
}

void decision()
{
    cout << "Again? Y/N\n";
    cin >> again;
    while (again == "y" || "Y" || "yes" || "Yes")
       {system("cls");
       main();}
    if (again == "n" || "N" || "no" || "No")
         {getch();}
    else
        { cout << "Invalid answer";
        decision();}
}


is there anything wrong here?
even if i use 'n' or any other letter it still clears the screen and goes back to main().
Last edited on
is there anything wrong here?

Yes.
"main()" should be "int main()".
You don't declare "decision", "again" and don't include <iostream>, unless you just omitted that part..
You're abusing recursion. Recursively calling main() is a terrible practice, recursively calling decision is not needed either. This functionality should instead be implemented with loops.
Your main problem is that, unless again is an std::string, == doesn't do what you want. String literals are of type const char* (they are pointers) and when you compare two pointers , you compare the addresses they point to, not the objects. You should instead use strcmp().
ISO C++ forbids calling '::main' from within program

again == "n" || again == "N" || again == "no" || again == "No" no de nuevo, decĂ­a
I'm sorry that code is incomplete.
I cut the portion that I am having trouble.
I'll post the whole so you could correct all my mistakes.


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
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string>
using namespace std;

void one();
void two();
void three();
void decision();

int num1, first1, second1, third1;
string first, second, third, again;

int main()
{
    cout << "Input a 3 digit number: ";
    cin >> num1;
    first1 = num1 / 100;
    second1 = num1 % 100 / 10;
    third1 = num1 % 100 % 10;
    if (first1 >= 10)
              {cout << "Invalid number" << endl;
              getch();
              return 0;}
    one();
    two();
    three();
    cout << "The roman numerical equivalent of this number is: " << first << second << third << endl;
    decision();
    return 0;
}

void decision()
{
    cout << "Again? Y/N\n";
    cin >> again;
    while (again == "y" || "Y" || "yes" || "Yes")
       {system("cls");
       main();}
    if (again == "n" || "N" || "no" || "No")
         {getch();}
    else
        { cout << "Invalid answer";
        decision();}
}

void one()
{
    if (first1 == 9)
    first = "CM";
    if (first1 == 8)
    first = "DCCC";
    if (first1 == 7)
    first = "DCC";
    if (first1 == 6)
    first = "DC";
    if (first1 == 5)
    first = "D";
    if (first1 == 4)
    first = "CD";
    if (first1 == 3)
    first = "CCC";
    if (first1 == 2)
    first = "CC";
    if (first1 == 1)
    first = "C";
    if (first1 == 0)
    first = "";
}
void two()
{
    if (second1 == 9)
    second = "XC";
    if (second1 == 8)
    second = "LXXX";
    if (second1 == 7)
    second = "LXX";
    if (second1 == 6)
    second = "LX";
    if (second1 == 5)
    second = "L";
    if (second1 == 4)
    second = "XL";
    if (second1 == 3)
    second = "XXX";
    if (second1 == 2)
    second = "XX";
    if (second1 == 1)
    second = "X";
    if (second1 == 0)
    second = "";
}
void three()
{
    if (third1 == 9)
    third = "IX";
    if (third1 == 8)
    third = "VIII";
    if (third1 == 7)
    third = "VII";
    if (third1 == 6)
    third = "VI";
    if (third1 == 5)
    third = "V";
    if (third1 == 4)
    third = "IV";
    if (third1 == 3)
    third = "III";
    if (third1 == 2)
    third = "II";
    if (third1 == 1)
    third = "I";
    if (third1 == 0)
    third = "";
}
If you removed the recursion, you wouldn't be having this problem.
This is because again == "y" || "Y" || "yes" || "Yes" doesn't do what you think. It should be again == "y" || again == "Y" || etc. The way you have it, it is always true.
Last edited on
it worked. thanks. :)
Topic archived. No new replies allowed.