cursed cursor....

Hello guys,
My first post here as i have only just joined your esteemed forum. So congrats for being so helpful to everyone. I have read many posts which have been very helpful.
I started programing a week ago, and for a middle aged man thats no mean feat !
Thankfully, i found your forum quite quickly. I read the tutorials and am finding the whole experience of programming fascinating to say the least.
So i am writing my first program which is for personal use so that i can enter my golf round scores. The program lists the scores and tells me if i have a birdie, par, etc for each hole. Then asks for my handicap and gives back a message if i have 'shot' under or over my handicap along with a brief 'pat on the back' or a ' Oh dear you were over par today'.
So on with my question.... Whilst validating my score input, i cannot seem to get the cursor to return to the same place to capture the next attempt.
It just goes straight to the next line !
Could you advise please.
Thanks.
Here's the part of the code..

(hmm... I can't seem to get the indentation correct in this box. I copied and pasted. Is there another way to get it the same as i wrote it ? )


int GetScores (int numarray[])
{
int x =0;
int i = 0;
int y =0;
int array[ARRAY_MAX];
for ( x = 1; x <19; x++ )
{
cout << x << ": " ;
string input =" ";

while (true) // Validate entry between 1 -9
{
getline(cin, input);
stringstream qwerty(input);
if (qwerty >> y && y > 0 && y <10)
break;
}
array[i] = y;

WhatPars(i,y); // call function to process Pars, birdies etc
i++;
} //end for loop
Last edited on
(hmm... I can't seem to get the indentation correct in this box. I copied and pasted. Is there another way to get it the same as i wrote it ? )

Yep: select the code and press the <> button.

About your question: afaik there's no option to let the cursor return, the console isn't ment for professional text editing and such. Do you really need it to return?
Last edited on
Thanks for the reply Fansje. I suppose i just have to make sure i input correctly. I was trying prevent the program looking untidy as the scores print out in a vertical line from top to bottom. If i accidently input any invalid data, it makes the whole line mess up.

Shall i post all the code... you can then try it and you'll see what i mean if you input a char instead of an integer ?

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
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int WhatPars(int i, int x);
void Title ();
int GetScores(int numarray[]);

const int ARRAY_MAX = 18;

int main()
{
    Title(); //call function to write title to screen
    int ScoreArray [ARRAY_MAX]; 
    GetScores(ScoreArray); // call function to collect scores into array
    cout << endl;
    cout <<" Press any Key then <Enter> to Finish\n";
   
    char f ;
    cin >> f ;
    return 0;
}
//*******************   T I T L E    **************************
void Title ()
{
    cout << "                   Golf Scores for Crowlands Heath \n";
    cout << "                              Par 66 \n"; 
    cout << "\n";          
}
//****************   Get Scores from User  *****************
int GetScores (int numarray[])
{      
    int x =0;
    int i = 0;
    int y =0;
    int array[ARRAY_MAX];
    for ( x = 1; x <19; x++ )
       {
       cout << x << ": " ;
       string input =" ";
       while (true)  // Validate entry between 1 - 9
       {
         getline(cin, input);
         stringstream qwerty(input);
         if (qwerty >> y && y > 0 && y <10) 
           break;
       }
        array[i] = y;
           
        WhatPars(i,y);  // call function to process Pars, birdies etc
        i++;
      } //end for loop
        //***************** Add Scores together  *******************
       int yourscore =0;
       x = 0 ;
       for ( x =0; x < ARRAY_MAX; x++ )
         {
       yourscore += array[x];
         }               
         cout << "\n Your Score Today: " << yourscore  <<" \n";
         cout << endl;
         cout << " What's your Handicap ? : ";
       int hndcp =0;
       string input =" ";
       while (true)  // Validate entry between 1 -28
         {
           getline(cin, input);
           stringstream qwerty(input);
           if (qwerty >> hndcp && hndcp > 0 && hndcp <29) 
              break;
         }
       int handicapscore = yourscore - hndcp;
       int h = 66 - handicapscore;
       int m = handicapscore - 66;
          if (handicapscore < 66)
             cout <<"\n Great Round ! You were " << h << " Under Par\n";
          else if (handicapscore > 66)
             cout <<"\n Oh dear ! You were " << m << " Over Par\n";
          else
             cout <<"\n Well done ! You were Level Par with your handicap\n";
             cout << endl; 
}  
//****************  What are Pars, bogeys, birdies etc  *****************
int WhatPars(int i, int x)
{
     int y =0;
     int ParArray[ARRAY_MAX] = {4,3,4,4,3,3,4,3,5,4,3,4,4,3,3,4,3,5 };
      // (y) is the array number which refers to par 
      // (x) is the score coming in
     y = ParArray[i]; 
     if  
     (x == y - 3 )
     cout <<"           Albatross\n";
     else if ( x == y - 2 )
     cout << "           Eagle\n";
     else if ( x == y - 1 )
     cout << "           Birdie\n";
     else if (x == y )
     cout << "           Par\n";
     else if (x == y + 1 )
     cout << "           Bogey\n";
     else if (x == y + 2 )
     cout << "           Double Bogey\n";
     else if (x == y + 3 )
     cout <<"           CRAP\n";
     else if (x == y + 4 )
     cout <<"           CRAP\n";
     else if (x == y + 5 )
     cout <<"           CRAP\n";
     else if (x == y + 6 )
     cout <<"           CRAP\n";
}
Last edited on
OK, I understand the problem now. It is in this part:
1
2
3
4
5
6
7
while (true)  // Validate entry between 1 - 9
{
    getline(cin, input);
    stringstream qwerty(input);
    if (qwerty >> y && y > 0 && y <10) 
        break;
}


Say you enter (so the cursor goes to the next line!) 'hello' instead of a number.
- getline stores 'hello' into input;
- your is-the-input-valid-check fails
- the loop starts again, now getting the input from the next line (because you pressed <Enter> at 'hello')

I don't know a way to prevent this from happening. Maybe you can try to first get the 19 inputs and then show the list with 'birdie', 'par' etc.
I think I'm gonna try something completely different. I don't like it as it is, it's very messy. Plus when it prints 'par, 'birdie, etc, it's not even on the same line. I will have to have a think about it.
Thanks for your input.
Topic archived. No new replies allowed.