Hello, I am having some trouble on a light program. Ive spent the last 5 hours on it, and I'm a beginner so I'm relatively slow. =)

I will show you what I have, but first I'll explain to you what I'm doing.

I am writing a numerology program.

I ask the user to input a date, in the format (DD-MM-YYYY)

After validating that the dates are logical, I must then add the digits together until they form one single digit. For example:

Date: 01-13-1967

0+1+1+3+1+9+6+7=28, then 2+8=10, and finally 1+0=1.

After gaining my condensed number, I plug it into a switch statement to get a quote. Just a fun little code.

Anyways, my problem lies with the crunching of the numbers! I cant figure out how to take a user input and crunch it down.

The closest I came...
I made a few 'char' variables and used a few as dummies to eliminate the dashes in the date.

It was easy the first round with cin user input, but how would I set char values when the program was being run?

Was I even on the right track there? Is there an easier way?

(Haha its a mess I know, just playing around with ideas)

Heres what I tried:

#include <iostream>
using namespace std;

int main ()
{
    

int m;
int z; 

char a;
char b;
char c;
char d;
char e;
char f;
char g;
char h;
char i;
char j;

z=-1;

  cout << "Please, enter your birthday: ";
  cin >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j;
  
  cout << a << b;
  cout << d << e;
  cout << g << h << i << j << endl;
  
  
  
  system ("PAUSE");
  return 0;
}

Here is my complete code so far. I have the date validation DONE but I just need to get the number crunching down.

#include <iostream>

using namespace std;



int main ()
{
    
    
    
    int d;
    int m;
    int y;
    int z;
    
    char A;
    char B;
    
    cout << "Enter your birthdate (DD-MM-YYYY): ";
    cin >> d 
    
    cout<< d 
    z=0;
    
do
{
    if(m==2) //FEBRUARY
    {
       if ((y%400==0) || (y%100==0) || (y%4==0)) //LEAP YEAR
       {
          if( (d<1) || (d>29) || (y<1875) || (y>2199) ) //VALIDATION
          {
          z=z+1;
          }
       }
       else // NO LEAP YEAR
       {
          if ( (d<1) || (d>28) || (y<1875) || (y>2199) ) //VALIDATION
          {
          z=z+1;
          }
       }
    }
    
    else // ANY OTHER MONTH BESIDES FEBRUARY
    {    
       if(m<7 ) //JANUARY TO JUNE
       {
          if(m%2==0) // JANUARY TO JUNE: EVEN MONTHS
          {
             if( (d<1) || (d>30) || (y<1875) || (y>2199) )
             {
             z=z+1;
             }     
          }       
          else // JANUARY TO JUNE: ODD MONTHS
          {
            if( (d<1) || (d>31) || (y<1875) || (y>2199) )
             {
             z=z+1;
             }         
          }
       }
       else //JULY TO DECEMBER
       {
          if(m%2==0) //JULY TO DECEMBER: EVEN MONTHS
          {
             if( (d<1) || (d>31) || (y<1875) || (y>2199) )
             {
             z=z+1;
             }     
          }
          else //JULY TO DECEMBER: ODD MONTHS
          {
             if( (d<1) || (d>30) || (y<1875) || (y>2199) )
             {
             z=z+1;
             }     
          }
       }
    }
}while (z > 0);



// NUMBER CRUNCHING GOES HERE!!!!




switch (x)
    {
           case '0'
                cout << x << "Good things, when short, are twice as good." << endl;
                break;
           
           case '1'
                cout << x << "I have found the best way to give advice to your children is to find out what they want and then advise them to do it." << endl;
                break;
                
           case '2'
                cout << x << "He only profits from praise who values criticism." << endl;
                break;
                
           case '3'
                cout << x << "Whatever advice you give, be brief." << endl;
                break;
                
           case '4'
                cout << x << "He who is too busy doing good finds no time to be good." << endl;
                break;
                
           case '5'
                cout << x << "Go put your creed into the deed or speak with double tongue." << endl;
                break;
                
           case '6'
                cout << x << "Write it on your heart that every day is the best day in the year. No man has learned anything rightly, until he knows that every day is Doomsday" << endl;
                break;
                
           case '7'
                cout << x << "In three words I can sum up everything I've learned about life: it goes on." << endl;
                break;
                
           case '8'
                cout << x << "If better were within, better would come out." << endl;
                break;
                
           case '9'
                cout << x << "Never put off until tomorrow what you can do today." << endl;
                break;
                
           case '0'
                cout << x << "Pride costs us more than hunger, thirst, and cold." << endl;
                break;
    }

I realize it all need polishing. I'm just stuck at a mental brick wall!

Any help is appreciated. Thank you!

Enter the date using a std::string object

#include <string> 
...
int main()
{
    std::string date;
    cin >> date;
   ...
}

now just use the isnumeric() macro to see if the digit is a digit or not. If a digit subtract '0' from the digit and add the int result to an accumulator, much like finding the sum of any string of integers. You don't need that switch statement at all -- just do it in a loop, and you should be able to code it in less than 5 lines or so.

Validating the string is a little more complicated. You have to split it up into its individual parts -- mm dd and yyyy -- then validate the integers somthing like what you already posted.

First of all, how about variables named day, month, and year instead of 1 letter cryptic names. And what is z for? Are you sure your date validation works?

Look up the % operator to get the individual digits of each value so you can add them up. There are many threads here that show how to do this. Search for modulus operator.

Look up the % operator to get the individual digits of each value so you can add them up. There are many threads here that show how to do this. Search for modulus operator.

Clarify: only needed when input is an integer, not a string. With integer input you can not enter the format "dd-mm-yyyy" because it won't accept the hyphens. You have to use string input for that.

#include <iostream>
#include <string>
#include <sstream>
using namespace std ;

int main()
{
  const char SEPERATOR = '-' ;
  string date_string ;
  getline( cin, date_string ) ; // dd-mm-yyyy ;
  istringstream stm(date_string) ;
  int day=0, month=0, year=0 ;
  char seperator1 = 0, seperator2 = 0 ;
  stm >> day >> seperator1 >> month >> seperator2 >> year ;
  // validate that seperator1 and seperator2 are == SEPERATOR
  // validate day, month, year values
  // add up digits
  // etc.
}

Clarify: only needed when input is an integer, not a string. With integer input you can not enter the format "dd-mm-yyyy" because it won't accept the hyphens. You have to use string input for that.

Well, with the posted code

if(m==2) //FEBRUARY
    {
       if ((y%400==0) || (y%100==0) || (y%4==0)) //LEAP YEAR
       {
          if( (d<1) || (d>29) || (y<1875) || (y>2199) ) //VALIDATION
          {
          z=z+1;
          }
       }
       else // NO LEAP YEAR
       {
          if ( (d<1) || (d>28) || (y<1875) || (y>2199) ) //VALIDATION
          {
          z=z+1;
          }
       }
    }

I figured the % operator would be usable. :icon_wink:

This article has been dead for over six months. Start a new discussion instead.