Make console loop the program until user is ready to quit

So im making a program that would translate date from dd/mm to it is <day> of <month>

I want to let the user make as many translations as he wants before quiting

how can i replace :
1
2
system "PAUSE"; 
return 0;
with a cammand that lets the user type exit to exit the console program >
1
2
3
4
5
while(true) {
    std::cout << '>';
    // get input
    // evaluate input; if it's the string "exit" break from the loop, else print the results
}
ok but how to input this ?

here is the source code
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
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
int nMonth;
cout << "Please Enter the day and Month: ";
cin >> nMonth;


switch (nMonth)

{   case 1/1:

    cout << "Its the first of January" << endl ;
    break;
    case 2/1:
   cout << "Its the second od january " << endl;
    break;
    case 3/1:
    cout << "Its the third of January" << endl;
    break;
    case 4/1:
    cout << "Its the fourth of Janury" << endl;
    break;
    case 5/1:
    cout << "Its the fifth of January" << endl;
    break;
    case 6/1:
    cout << "Its the sixth of January" << endl;
    break;
    case 7/1:
    cout << "Its the sevent of January" << endl;
    break;
    case 8/1:
    cout << "Its the eight of January" << endl;
    break;
    case 9/1:
    cout << "Its the ninth of January" << endl;
    break;
    default:
    cout    << "This is not valid input"
            << " Restart and try again"
         << " Remember the format must be dd/mm" <<endl;
}

system ("PAUSE");
return 0;
}
First you need to get your code right. I'd immediately ditch hungarian notation and never ever use it again. Also, I'd name main's parameters the conventional way, argc (argument count) and argv(argument vector). Just get used to them.

Next if you want to parse input like the string "exit" you can't just read everything into an int. Using an int also makes the input "d/m" read only d, while leaving "/m" in the stream, because '/' is not a digit.

Also notice that when you say case 3/1:, you're saying case 3:, because the integer division 3 / 1 evaluates to 3 (3 / 2 would evaluate to 1).

I suggest you read user input into a string. If it's the string "exit", you can figure out what to do. If it isn't, use a stringstream to parse it, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string s;
std::cin >> s;
if(s == "exit") {
    // code
}
else {
    std::stringstream ss(s);  // constructs a stringstream from what's in s
    int day, month;
    char separator;
    if(ss >> day >> separator >> month && separator == '/') {
        // it's a valid expression (you should test for the validity of day and month too)
        // evaluate and output the result
    }
    else {
        // the format is not "integer/integer"
    }
}

On another note, when you have to match an integer with an ordered set of strings, you can do this:

1
2
const std::string ordinals[] = { "first", "second", /* ... */ };
const std::string months[] = { "January", "February" /* ... */ };

So you can then refer to the elements using the input passed by the user, like this:

std::cout << "It's the " << ordinals[day - 1] << " of " << months[month - 1] << std::endl;
Last edited on
Thanks ,

I know my code is not very good but i only started coding yesterday from a c++ book , thanks for the help anyway i like the idea to list the days and months
Hey Filipe i cleaned up my code thanks to you here is the new one
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
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main (int nNumberofArgs, char* pszArgs [] )
 {
 int month;
 int day;
 cout << "Enter the day of the month: ";
 cin >> day;
 cout << "Enter the Number of the Month: ";
 cin >> month;

 const std::string ordinals[] = { "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eight", "nineth", "tenth", "eleventh",
  "Twelveth", "thirteenth", "Fourteenth", "Fiftheenth", "sixteenth", "sevententh", "Eighteenth", "Nineteenth", "twenteeth", "Twenty-first",
  "Twenty-Second", "twenty-third", "twenty-fourth", "Twenty-fifth", "Twenty-sixth", "Twenty-Seventh", "Twenty-eight", "Twnety-ninth", "Thirthyth",
   "Thirthyth-First" };

 const std::string months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
 "November", "December"};



std::cout << "It's the " << ordinals[day - 1] << " of " << months[month - 1] << std::endl;


 system ("PAUSE");
 return 0;
 }

Last edited on
Topic archived. No new replies allowed.