boolean loop

It compiled perfectly until the last part where I must answer Y/N for Another?... I type y for yes but it just went straight to calculating the y and not letting me type a new or word...

not sure if I overlooked something or there's a small error.

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
#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <cstring>


using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ios;
using std::ostringstream;

//function prototypes
void getString();
void convertString();
int totalSum();
bool checkValue(int);
void outputString(int);

//global variables
string stringer = "";
int *out = new int[256];
int size = 0;

int main()
{
  //Sets IOS flag for the rest of the program.
    cout << std::setiosflags(ios::fixed);
    bool cont = true;
    
    while(cont)
    {
        getString();
        convertString();
        bool cont2 = false;
        
        while(!cont2)
        {
            stringer = "";
            ostringstream s;
            int check = totalSum();
            s << check;
            stringer = s.str();
            
            convertString();
            
            cont2 = checkValue(totalSum());
        }
        
        outputString(totalSum());
        
        cont = false;
        cout << "Another? (y/n)";
        char response;
        cin.sync();
        cin >> response;
        if((response == 'y') || (response == 'Y'))
        {
            cont = true;
        }
        
        cout << endl;
        
    }
    
    return 0;
}


//get input from user and clear previous input
void getString()
{
    stringer = "";
    cout << "Enter a name: ";
    cin.clear();
    cin.sync();
    getline(cin, stringer);
}

// convert string into proper numerical values
void convertString()
{
    delete[] out;
    out = new int[256];
    long size = stringer.length();
    for(int i = 0; i < size; i++)
    {
        out[i] = 0;
        if(isalpha(stringer[i]))
        {
            stringer[i] = tolower(stringer[i]);
            out[i] = stringer[i] - 96;
        }
        else if(isdigit(stringer[i]))
        {
            out[i] = stringer[i] - 48;
        }
    }
}

// convert string into proper numerical values
int totalSum()
{
    int total = 0;
    for(int i = 0; i < size; i++)
    {
        total += out[i];
    }
    return total;
}

//checks if value is in the right range
bool checkValue(int total)
{
    if((total == 11) || (total == 12) || (total == 22))
    {
        return true;
    }
    else if((total < 0) || (total > 9))
    {
        return false;
    }
    else
    {
        return true;
    }
}
Last edited on
closed account (48T7M4Gy)
Show all the code please, with the includes etc. Just edit them back into your post.
@kemort I think the issue is only in the int main(). because the other functions still good.
doesn't work....
closed account (48T7M4Gy)
If you don't want to include everything and play hide and seek then you're only making it difficult for yourself. Where is outputString()?

I'll give you a kindly tip. If you are so sure it's in a small section, and I don't doubt your word, then just carve that bit out and test that section. Paste a workable (albeit failing) test section here. The turnaround is very fast if you do it this way. Once it works then splice it back in. It is like unit testing.

If you want help on the test unit then feel free. Time's moving on :)
I tried playing it around by using breakpoint... and moving around the if(response=='y' || 'Y') still didn't get the same result i'm looking for....
closed account (48T7M4Gy)
So, what's the story?

You can't or won't paste in the missing outputString function, so we can't run your program?

Or is it you can't cut and paste the test snippet of the section you are so sure is wrong?

Or you just want to complain about it?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//outputString(totalSum());
        
        cont = false;
        cout << "Another? (y/n)";
        char response;
        //cin.sync(); // <---
        cin >> response;
        cin.ignore(1000, '\n'); // <--
        cin.clear(); // <---
        
        if((response == 'y') || (response == 'Y'))
        {
            cont = true;
        }
        
        cout << "We're done - hide and seek is over" << endl; //<--
        
closed account (48T7M4Gy)
It's your lucky day! I forgot to show the output after the fix ... which is close to what you want/need

Enter a name: Hide and seek
Another? (y/n)y
We're done - hide and seek is over
Enter a name: Seek and Hide
Another? (y/n)N
We're done - hide and seek is over
 
Exit code: 0 (normal program termination)


Hint: shift exit message down a line or two to reflect what happens at end.
Enter a name: Hide Seek
Another? (y/n)Y
Enter a name: Seek Hide
Another? (y/n)N
We're done - hide and seek is over
 
Exit code: 0 (normal program termination)
Last edited on
@kemort

Thanks that actually works... You're the man!

question is why the can.ignore(1000, '\n') ???
better question is what exactly does that do besides the term ignore?
closed account (48T7M4Gy)
Good. Sorry to give you a hard time but it's mainly to enable you to do some testing yourself instead of having to grapple with many many lines.

Always remember, it's a well known killer, if you mix cin with getline you have to 'clear the stream' with ignore and reset the stream fail with clear. You can read up on it when you have time, but if you input a string with a blank space in it the stream reads there are two strings because of the space. So you need to clear the junk and then use getline.

1000 is to make sure you miss nothing, gentle is right with the more complicated limit stuff - it blitzes with absolute certainty, 1000 doesn't, some ppl use 256 but not good.

Perhaps even write it down, thousands have lost sleep, left home, given up, spoken badly to domestic animals, and even worse for not knowing/remembering.

Cheers

http://www.cplusplus.com/reference/istream/basic_istream/ignore/
Last edited on
It's fine you give me a hard time... I need the push to find alternatives and push myself harder. I like the challenge and to learn as much as possible in a day or in a week. So thanks pal!! I appreciate it, I did feel like I didn't learn as much as I thought I should from my Intermediate class. Like some codes, I had some help along the way. The Basic C++ was easy for me, but the Intermediate C++ just started, and I feel like there's a gap where I should know some things before doing the assignment....

So again thanks for the hard time or the push( positive ).
Last edited on
Topic archived. No new replies allowed.