Notes from 10/22

a7sol1.cpp

//By Oscar Castillo
//Date: 10/21/2012
#include <iostream>
#include <string>
using namespace std;
int main ()
{
   int multiply;
   int add;
   int n, sum = 0, count = 0;
   cout<<"Would you like to add or multiply? ";
   string choice;
   getline(cin, choice);
   while(choice != "add" && choice != "multiply")
   { 
       cout<< "Sorry, I don't know how to " << choice <<endl;
       cout<<"Would you like to add or multiply? ";
       getline(cin, choice);
   }
   cout<<"Enter one number per line, 0 to stop : "<<endl;
   if(choice == "add")
   {
       int accum = 0;
       cin >> n;
       while( n > 0 )
       {
           accum += n;
           cin >> n;
       }
       cout << "The sum is " << accum << endl;
   }
   else // multiply
   {
       int accum = 1;
       cin >> n;
       while( n > 0 )
       {
           accum *= n;
           cin >> n;
       }
       cout << "The product is " << accum << endl;
   }

   return 0; 
}
//the problem with this program is that i couldn't figure out how to stop it at 0 and that the addition and multiplicaion 
//output the wrong answer.

a7sol2.cpp

#include <iostream>
using namespace std;
int main()
{
    string choice;
    while(true)
    {    
        cout << "Would you like to add (enter 'a') or multiply (enter 'm')? ";
        getline(cin, choice);

        if(choice == "a")
        {
           cout << "Enter one number per line, 0 to stop:";
           int num;
           int acc = 0;
           while(num != 0)
           {
                cin >> num;
                acc = acc + num; 
           }
           cout << "Thank You. The sum is " << acc << ".";
           break;

        }
        if(choice == "m")
        {
            cout << "Enter one number per line, 1 to stop:";
           int num;
           int acc = 1;
           while(num != 1)
           {
                cin >> num;
                acc = num * acc; 
           }
           
            cout << "Thank You. The product is " << acc << ".";
           break;

        }
        // At this point, they typed something wrong.
        // Condition of neither a nor m is redundant.

        cout << "Sorry. I don't know how to " << choice << ".\n";
    }
    return 0;
}

a7sol3.cpp

//By Oscar Castillo
//Date: 10/21/2012
#include <iostream>
#include <string>
using namespace std;
int main ()
{
   int multiply;
   int add;
   int n, sum = 0, count = 0;
   cout<<"Would you like to add or multiply? ";
   string choice;
   getline(cin, choice);
   while(choice != "add" && choice != "multiply")
   { 
       cout<< "Sorry, I don't know how to " << choice <<endl;
       cout<<"Would you like to add or multiply? ";
       getline(cin, choice);
   }
   cout<<"Enter one number per line, 0 to stop : "<<endl;
   int accum;
   if(choice == "add") accum = 0;
   else accum = 1;

   cin >> n;
   while( n > 0 )
   {
       if(choice == "add") accum += n;
       else accum *= n;
       cin >> n;
   }

   string word;
   if(choice == "add") word = "sum";
   else word = "product";

   cout << "The " << word << " is " << accum << endl;

   return 0; 
}
//the problem with this program is that i couldn't figure out how to stop it at 0 and that the addition and multiplicaion 
//output the wrong answer.

continue.cpp

#include <iostream>
using namespace std;
int main()
{
    for(int i = 1; i <= 12; i++)
    {
        if(i%4 == 0) {
            cout << "BANG!\n";
            break;  // continue;
        }
        cout << i << endl; // skipped when i%4 == 0
    }
    
    return 0;
}

days.cpp

#include <iostream>
using namespace std;
int main()
{
    for(int i = 1; i <= 12; i++)
    {
        cout << "\nOn the " << i;
        switch(i) {
        case 1: cout << "st"; break;
        case 2: cout << "nd"; break;
        case 3: cout << "rd"; break;
        default: cout << "th";
        }
        cout << " day of Xmas\n";
        switch(i)
        {
        case 6: cout << "Six geese\n";
        case 5: cout << "Five golden rings\n";
        case 4: cout << "Four calling birds\n";
        case 3: cout << "Three french hens\n";
        case 2: cout << "Two turtle doves\n";
        case 1: cout << "Partridge\n";
        }
    }
    return 0;
}

nested.cpp

#include <iostream>
using namespace std;
int main()
{
    for(int i = 3; i <= 8; i++) 
    {
        for(int j = 1; j < i; j++) 
        {
            cout << "*";
            //cout << i << ", " << j << endl;
        }
        cout << "\n";
    }

    for(int i = 0; i <= 10; i++) {
        for(int j = 0; j <= 10; j++) {
            if(((i+j)/2)%2 == 0) {
                cout << "*";
            }
            else {
                cout << " ";
            }
        }
        cout << "\n";
    }


     return 0;
}