New Posts  All Forums:Forum Nav:

Test me on C++ - Page 2

post #11 of 32
Thread Starter 
Quote:
Originally Posted by BradleyW View Post

You did OK, don't worry.


Take another look at QUESTION ONE.

Ahh biggrin.gif Ok question one hmmm....
Code:
#include <iostream>
using namespace std;

int addNumbers(int no1, no2);
{

cout <<what is 1st num?<<endl;
cin >> no1;

cout <<what is 2nd num?<<endl;
cin >> no2;

int no3=no1+no2;
return no3;
}

int main()
{

  cout<< addNumbers();


}

My best guess lol
Work PC
(4 items)
 
Home PC
(4 items)
 
 
CPUMotherboardGraphicsRAM
4770k Asus SABERTOOTH Z87 290 16 trident  
CPUMotherboardGraphicsRAM
8350 gigabyte 990fxa 660 16gb trident 
  hide details  
Reply
Work PC
(4 items)
 
Home PC
(4 items)
 
 
CPUMotherboardGraphicsRAM
4770k Asus SABERTOOTH Z87 290 16 trident  
CPUMotherboardGraphicsRAM
8350 gigabyte 990fxa 660 16gb trident 
  hide details  
Reply
post #12 of 32
Thread Starter 
Quote:
Originally Posted by ADHDadditiv View Post

Oh god its been a while, I'll try though.
Code:
int addNumbers(no1, no2)
{
int no3=no1+no2;
return no3
int anArray[5] (something to do with no3 going into the array)
}
int show(Last)
{
Main = Last(call from the Array);
return =  Main;
Thats probably wrong, but it should be close.

I kinder get where this code was going but the last part confused me...
Work PC
(4 items)
 
Home PC
(4 items)
 
 
CPUMotherboardGraphicsRAM
4770k Asus SABERTOOTH Z87 290 16 trident  
CPUMotherboardGraphicsRAM
8350 gigabyte 990fxa 660 16gb trident 
  hide details  
Reply
Work PC
(4 items)
 
Home PC
(4 items)
 
 
CPUMotherboardGraphicsRAM
4770k Asus SABERTOOTH Z87 290 16 trident  
CPUMotherboardGraphicsRAM
8350 gigabyte 990fxa 660 16gb trident 
  hide details  
Reply
post #13 of 32
Well done for QUESTION ONE. Only other thing I expected is that you use the & symbol to allow special read/write privileges for the variables. Also, it is good practise to have int main at the top of the program. In addition, you did not really need to change the order of the program as such. So, you get 4 out of 5 marks. Here is what would net you full marks. btw, FYI, your doing awesome!
Code:
#include <iostream>
using namespace std;

int main()
{
   int no1, no2;
   cout << "Input First Number";
   cin >> no1;
   cout << "Input Second Number";
   cin >> no2;
   int addNumbers(int& no1, int& no2);
   cout << addNumbers(no1, no2);
}

int addNumbers(int& no1, int& no2)
{
   return no1 + no2;
}

BTW: Here is a little program I made. It will help you understand how to pass variables in different ways. Take a look. Warning: Spoiler! (Click to show)
Code:
//Written By Bradley Worthington - Copyright Protected
#include <iostream>
using namespace std;
int main()
{
        double a(0), b(0); int option(0);
        void getNumbers(double& a, double& b); 
        void getNumber(double& a); 
        void temps(double& a, double& b, int& option);
        void measurements(double& a, double& b, int& option);
        void calc(double& a, double& b, int& option);
        void currency(double& a, double& b, int& option);

        cout << "Main Menu\n\n";
        cout << "Please select an option below\n\n";
        cout << "1. Convert Temperatures\n";
        cout << "2. Convert Measurements\n";
        cout << "3. Basic Calculator\n";
        cout << "4. Currency Converter (Updated as of April 2014)\n"; 
        cout << "5. Quit\n";
        cin >> option;

        while ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }

        if (option == 1)
                temps(a, b, option);
        if (option == 2)
                measurements(a, b, option);
        if (option == 3)
                calc(a, b, option);     
        if (option == 4)
                currency(a, b, option);
        if (option == 5) 
                system ("pause");
}

void getNumbers(double& a, double& b)
{
        cout << "Input First Number\n";
        cin >> a; 
        cout << "Input Second Number\n";
        cin >> b;
}

void getNumber(double& a)
{
        cout << "Enter a value \n";
        cin >> a;
}

void temps(double& a, double& b, int& option)
{
        void CtoF(double& a, double& b);
        void FtoC(double& a, double& b);
        
        cout << "Please select an option below\n\n";
        cout << "1: Convert celcius to farenheit\n";
        cout << "2: Convert farenheit to celcius\n";
        cout << "3: Main Menu\n";
        cin >> option;
        while ((option != 1) && (option != 2) && (option != 3))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }
        if (option == 1)
        {
                getNumber(a);
                CtoF(a, b);
                cout << a << " Celcius = " << b << " Farenhiet\n\n";
                temps (a, b, option);
        }
        if (option == 2)
        {
                getNumber(a);
                FtoC(a, b);
                cout << a << " Farenhiet = " << b << " Celcius \n\n";
                temps (a, b, option);
        }
        else 
                option = 0;
                main();
}

void CtoF(double& a, double& b)
{
        b = (((a * 9) / 5) + 32); 
}

void FtoC(double& a, double& b)
{
        b = (((a - 32) * 5) / 9);
}

void measurements(double& a, double& b, int& option)
{
        void mmTocm (double& a, double& b);
        void cmTomm (double& a, double& b);
        void cmToinches (double& a, double& b);
        void inchesTocm (double& a, double& b);
        void metersTomiles (double& a, double& b);
        void milesTometers (double& a, double& b);
        void metersToyards (double& a, double& b);
        void yardsTometers (double& a, double& b);
        void circleCircumferece (double& a, double& b);
        void circleArea (double& a, double& b);
        void areaPerimeterOfSquareRectange (double& a, double& b);

        cout << "Please select an option below\n\n";
        cout << "1:  Convert mm to cm\n";
        cout << "2:  Convert cm to mm\n";
        cout << "3:  Convert cm to Inches\n";
        cout << "4:  Convert Inches to cm\n";
        cout << "5:  Convert Meters to Miles\n";
        cout << "6:  Convert Miles to Meters\n";
        cout << "7:  Convert Meters to Yards\n";
        cout << "8:  Convert Yards to Meters\n";
        cout << "9:  Calculate Circumference (cm) of a Circle\n";
        cout << "10: Calculate Area (cm2) of a Circle\n";
        cout << "11: Calculate Area (cm2) and Perimeter (cm) of a Square/Rectangle\n";
        cout << "12: Main Menu\n";
        cin >> option;

        while ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5) && (option != 6) && (option != 7) && (option != 8) && (option != 9) && (option != 10) && (option != 11) && (option != 12))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }

        if (option == 1)
        {
                getNumber(a);
                mmTocm(a, b);
                cout << a << "mm/s = " << b <<"cm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 2)
        {
                getNumber(a);
                cmTomm(a, b);
                cout << a << " cm/s = " << b <<" mm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 3)
        {
                getNumber(a);
                cmToinches(a, b);
                cout << a << " cm/s = " << b <<" Inche/s\n\n";
                measurements( a, b, option);
        }
        if (option == 4)
        {
                getNumber(a);
                inchesTocm(a, b);
                cout << a << " Inche/s = " << b <<" cm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 5)
        {
                getNumber(a);
                metersTomiles(a, b);
                cout << a << " Meter/s = " << b <<" Mile/s\n\n";
                measurements( a, b, option);
        }
        if (option == 6)
        {
                getNumber(a);
                milesTometers(a, b);
                cout << a << " Mile/s = " << b <<" Meters/s\n\n";
                measurements( a, b, option);
        }
        if (option == 7)
        {
                getNumber(a);
                metersToyards(a, b);
                cout << a << " Meter/s = " << b <<" Yard/s\n\n";
                measurements( a, b, option);
        }
        if (option == 8)
        {
                getNumber(a);
                yardsTometers(a, b);
                cout << a << " Yard/s = " << b <<" Meter/s\n\n";
                measurements( a, b, option);
        }
        if (option == 9)
        {
                circleCircumferece(a, b);
                cout << "Circumference (cm/s) of the Circle = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 10)
        {
                circleArea(a, b);
                cout << "Area (cm2) of the Circle = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 11)
        {
                areaPerimeterOfSquareRectange (a, b);
                cout << "Area (cm2) = " << a << " Perimeter (cm) = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 12) 
                main();
}

void mmTocm (double& a, double& b)
{
        b = (a / 10);
}
void cmTomm (double& a, double& b)
{
        b = (a * 10);
}
void cmToinches (double& a, double& b)
{
        b = (a / 2.50);
}
void inchesTocm (double& a, double& b)
{
        b = (a * 2.50);
}
void metersTomiles (double& a, double& b)
{
        b = (a / 0.000621371192);
}
void milesTometers (double& a, double& b)
{
        b = (a * 1609.344);
}
void metersToyards (double& a, double& b)
{
        b = (a * 1.0936133);
}
void yardsTometers (double& a, double& b)
{
        b = (a / 0.9144);
}
void circleCircumferece (double& a, double& b)
{
        double const PI = 3.14;
        cout << "Enter Diameter (cm/s)\n";
        cin >> a;
        b = (a * PI);
}
void circleArea (double& a, double& b)
{
        double const PI = 3.14;
        cout << "Enter Diameter (cm/s)\n";
        cin >> a;
        b = (a / 2); 
        b = (b * b * PI);       
}
void areaPerimeterOfSquareRectange (double& a, double& b)
{
        double temp(0);
        cout << "Enter Lenght (cm/s)\n";
        cin >> a;
        cout << "Enter Width (cm/s)\n";
        cin >> b;
        temp = a;
        a = (a * b);
        b = (temp * 2) + (b * 2);
}
void calc(double& a, double& b, int& option)
{
        double Addition(double& a, double& b);
        double Subtraction(double& a, double& b);
        double Multiplication(double& a, double& b);
        double Division(double& a, double& b);

        cout << "Please select an option below\n\n";
        cout << "1: Addition\n";
        cout << "2: Subtraction\n";
        cout << "3: Multiplication\n";
        cout << "4: Division\n";
        cout << "5: Main Menu\n";
        cin >> option;

        while ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }

         if (option == 1)
         {
                 getNumbers(a, b);
                 cout << a << " + " << b << " = " << Addition(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 2)
         {
                 getNumbers(a, b);
                 cout << a << " - " << b << " = " << Subtraction(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 3)
         {
                 getNumbers(a, b);
                 cout << a << " x " << b << " = " << Multiplication(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 4)
         {
                 getNumbers(a, b);
                 cout << a << " / " << b << " = " << Division(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 5) 
                 main();
}

double Addition(double& a, double& b)
{
        return a + b;
}
double Subtraction(double& a, double& b)
{
        return a - b;
}
double Multiplication(double& a, double& b)
{
        return a * b;
}
double Division(double& a, double& b)
{
        return a / b;
}

void currency(double& a, double& b, int& option)
{
        void gbpToeuro(double& a, double& b);
        void euroTogbp(double& a, double& b);
        void gbpTousd(double& a, double& b);
        void usdTogbp(double& a, double& b);

        cout << "Select An Option Below\n\n";
        cout << "1: Convert GBP to EURO\n";
        cout << "2: Convert EURO to GBP\n";
        cout << "3: Convert GBP to USD\n";
        cout << "4: Convert USD to GBP\n";
        cout << "5: Main Menu\n";
        cin >> option;

        while ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }

        if (option == 1)
        {
                getNumber(a);
                gbpToeuro(a, b);
                cout << a << " GBP = " << b << " EURO\n\n";
                currency(a, b, option);
        }
        if (option == 2)
        {
                getNumber(a);
                euroTogbp(a, b);
                cout << a << " EURO = " << b << " GBP\n\n";
                currency(a, b, option);
        }
        if (option == 3)
        {
                getNumber(a);
                gbpTousd(a, b);
                cout << a << " GBP = " << b << " USD\n\n";
                currency(a, b, option);
        }
        if (option == 4)
        {
                getNumber(a);
                usdTogbp(a, b);
                cout << a << " USD = " << b << " GBP\n\n";
                currency(a, b, option);
        }
        if (option == 5) 
                main();
}


void gbpToeuro(double& a, double& b)
{
        b = (a * 1.21);
}
void euroTogbp(double& a, double& b)
{
        b = (a * 0.83);
}
void gbpTousd(double& a, double& b)
{
        b = (a * 1.67);
}
void usdTogbp(double& a, double& b)
{
        b = (a * 0.60);
}

Edited by BradleyW - 4/21/14 at 3:02pm
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #14 of 32
Another Question For You.
Find and fix the errors in the code below without using the aid of a compiler. There are 4 errors to find and fix. (4 marks)
Code:
#include <iostream>
using namespace std;
int main()
{
   char choice;
   cout << Would you like a drink of tea? (Y/N) << endl
   cin >> choice;
   choice = (toupper(choice));

   if (choice = Y)
   cout << "\nHere is your tea";

   if (choice == N)
   cout << "\nOK, no problem";
   cout << "\nBye Bye";
   
system ("PAUSE");
}
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #15 of 32
Thread Starter 
Quote:
Originally Posted by BradleyW View Post

Well done for QUESTION ONE. Only other thing I expected is that you use the & symbol to allow special read/write privileges for the variables. Also, it is good practise to have int main at the top of the program. In addition, you did not really need to change the order of the program as such. So, you get 4 out of 5 marks. Here is what would net you full marks. btw, FYI, your doing awesome!

thanks dude! and nice code :O that is super long lol
Code:
#include <iostream>
using namespace std;
int main()
{
   char choice;
   cout << Would you like a drink of tea? (Y/N) << endl
   cin >> choice;
   choice = (toupper(choice));

   if (choice = Y)
   cout << "\nHere is your tea";

   if (choice == N)
   cout << "\nOK, no problem";
   cout << "\nBye Bye";
   
system ("PAUSE");
}

I didnt use a compiler at all wink.gif
error 1 and 2 cout << Would you like a drink of tea? (Y/N) << endl; (missing semi colon) and speech mark
error 3. cout << "\nBye Bye"; and cout << "\nOK, no problem"; and new line in the wrong side
Work PC
(4 items)
 
Home PC
(4 items)
 
 
CPUMotherboardGraphicsRAM
4770k Asus SABERTOOTH Z87 290 16 trident  
CPUMotherboardGraphicsRAM
8350 gigabyte 990fxa 660 16gb trident 
  hide details  
Reply
Work PC
(4 items)
 
Home PC
(4 items)
 
 
CPUMotherboardGraphicsRAM
4770k Asus SABERTOOTH Z87 290 16 trident  
CPUMotherboardGraphicsRAM
8350 gigabyte 990fxa 660 16gb trident 
  hide details  
Reply
post #16 of 32
I don't have any code, but I wish you good luck and steady nerves on your up coming test.
Primary
(18 items)
 
Secondary
(9 items)
 
Tertiary
(10 items)
 
CPUMotherboardGraphicsRAM
Ryzen 7 1700 ASUS Pro x370 Prime Gigabyte Windforce 1070 GSkill 3000MH C15 1.35V 
Hard DriveHard DriveHard DriveOptical Drive
Seagate 1TB (ST1000DM003 9YN162) Seagate 2TB (ST2000DM001 1CH164 Trion 150 960GB Some LG Blu-Ray 
CoolingOSMonitorMonitor
Noctua D14 Windows 7 Profession 64bit Acer V246HL Acer G245HQ 
MonitorKeyboardPowerCase
ASUS MX25AQ Logitec G710+ Rosewill Capstone 450W Cooler Master Haf XB EVO 
MouseMouse Pad
Roccat Lua My table 
CPUMotherboardGraphicsRAM
AMD FX 8350 ASUS Sabertooth 990FX EVGA 560TI ADATA 4x2GB 1600C9 1.65V-1.5V 
Hard DriveCoolingOSPower
Some 2.5in 1TB drive Stock Windows 7 Home Premium 64bit Corsair TX850 
Case
Cooler Master HAF 
CPUMotherboardRAMOptical Drive
i7-960 Gigabyte X58 ADATA 2x2GB 1600C9 1.65V-1.5V Generic DVD 
CoolingOSMonitorKeyboard
Asetek 510LC Linux with pfSense in VM Some generic 1280x1024 Generic 
CaseMouse
Corsair Carbide 400R Generic 
  hide details  
Reply
Primary
(18 items)
 
Secondary
(9 items)
 
Tertiary
(10 items)
 
CPUMotherboardGraphicsRAM
Ryzen 7 1700 ASUS Pro x370 Prime Gigabyte Windforce 1070 GSkill 3000MH C15 1.35V 
Hard DriveHard DriveHard DriveOptical Drive
Seagate 1TB (ST1000DM003 9YN162) Seagate 2TB (ST2000DM001 1CH164 Trion 150 960GB Some LG Blu-Ray 
CoolingOSMonitorMonitor
Noctua D14 Windows 7 Profession 64bit Acer V246HL Acer G245HQ 
MonitorKeyboardPowerCase
ASUS MX25AQ Logitec G710+ Rosewill Capstone 450W Cooler Master Haf XB EVO 
MouseMouse Pad
Roccat Lua My table 
CPUMotherboardGraphicsRAM
AMD FX 8350 ASUS Sabertooth 990FX EVGA 560TI ADATA 4x2GB 1600C9 1.65V-1.5V 
Hard DriveCoolingOSPower
Some 2.5in 1TB drive Stock Windows 7 Home Premium 64bit Corsair TX850 
Case
Cooler Master HAF 
CPUMotherboardRAMOptical Drive
i7-960 Gigabyte X58 ADATA 2x2GB 1600C9 1.65V-1.5V Generic DVD 
CoolingOSMonitorKeyboard
Asetek 510LC Linux with pfSense in VM Some generic 1280x1024 Generic 
CaseMouse
Corsair Carbide 400R Generic 
  hide details  
Reply
post #17 of 32
Thread Starter 
Quote:
Originally Posted by AcEsSalvation View Post

I don't have any code, but I wish you good luck and steady nerves on your up coming test.

Thanks dude, really appreciate it!
Work PC
(4 items)
 
Home PC
(4 items)
 
 
CPUMotherboardGraphicsRAM
4770k Asus SABERTOOTH Z87 290 16 trident  
CPUMotherboardGraphicsRAM
8350 gigabyte 990fxa 660 16gb trident 
  hide details  
Reply
Work PC
(4 items)
 
Home PC
(4 items)
 
 
CPUMotherboardGraphicsRAM
4770k Asus SABERTOOTH Z87 290 16 trident  
CPUMotherboardGraphicsRAM
8350 gigabyte 990fxa 660 16gb trident 
  hide details  
Reply
post #18 of 32
Quote:
Originally Posted by AnnoyinDemon View Post

thanks dude! and nice code :O that is super long lol
Code:
#include <iostream>
using namespace std;
int main()
{
   char choice;
   cout << Would you like a drink of tea? (Y/N) << endl
   cin >> choice;
   choice = (toupper(choice));

   if (choice = Y)
   cout << "\nHere is your tea";

   if (choice == N)
   cout << "\nOK, no problem";
   cout << "\nBye Bye";
   
system ("PAUSE");
}

I didnt use a compiler at all wink.gif
error 1 and 2 cout << Would you like a drink of tea? (Y/N) << endl; (missing semi colon) and speech mark
error 3. cout << "\nBye Bye"; and cout << "\nOK, no problem"; and new line in the wrong side

OK not bad, but look closer. You missed 2 errors. The \n's are fine. The program would still compile.
The errors you missed are the following:
Error 3 = Look at the first IF statement. Two equals signs are required. Only one equals sign is present.
Error 4 = The second IF statement requires { } body because it has more than 1 line of code associated.

Look at a fixed version of the code for more information:
Code:
#include <iostream>
using namespace std;
int main()
{
   char choice;
   cout << "Would you like a drink of tea? (Y/N)" << endl; //Fixed
   cin >> choice;
   choice = (toupper(choice));

   if (choice == Y) //Fixed
   cout << "\nHere is your tea";

   if (choice == N) //Fixed
   {
   cout << "\nOK, no problem";
   cout << "\nBye Bye";
   }
   
system ("PAUSE");
}
I can see why you might have missed error 4, because it's not really an error I suppose, so sorry about that mate. I guess overall you only missed 1 compile error. So I will give you 3 marks out of 4.
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #19 of 32
I've made a small program for you. It looks complicated, but what I'd like you to do is to review the code and explain what it does. My tip is to get pen and paper and write down what is going on step by step. Pretend you are the processor executing the code line by line. Write down what is happening to the information. Don't use a compiler as always smile.gif
Hope this helps you out.
Warning: Spoiler! (Click to show)
Code:
#include <iostream> 
#include <iomanip>
using namespace std;

int originalNumber;
int key;
int n1, n2, n3;
int encryptedNumber;

int main()  
{   

        cout << "Enter the original three-digit number:" << endl;  
        cin >> originalNumber;                                    

        cout << "Enter the key:" << endl;  
        cin >> key;                        

         void isolateDigits();            
          isolateDigits();                

         void replaceDigits();             
          replaceDigits();                

         void swapDigit1WithDigit3();      
          swapDigit1WithDigit3();          

         void recomposeEncryptedNumber();  
          recomposeEncryptedNumber();      

        cout.setf(ios::fixed);  
        cout << setfill('0');  
        cout << "The encrypted number for" << " " << originalNumber << " " << "is" << " " << setw(3) << encryptedNumber << "." << endl; 
        system("pause");        

};

void isolateDigits() 
{   
        n1 = ((originalNumber / 100) % 100); 
        n2 = ((originalNumber / 10) % 10);   
        n3 = (originalNumber % 10);          
}

void replaceDigits() 
{   
        n1 = ((n1 + key) % 10);  
        n2 = ((n2 + key) % 10);  
        n3 = ((n3 + key) % 10);  
}

void swapDigit1WithDigit3() 
{   
        int tempStorage;       
        tempStorage = n1;   
        n1 = n3;                
        n3 = tempStorage;      
}

void recomposeEncryptedNumber() 
{   
        encryptedNumber = (n1 * 100) + (n2 * 10) + (n3 * 1);                      
}
So again, review the code line by line and make use of pen and paper to help you.
Post your results.

When you've finished with that, have a look at this code I made for you. Again, use pen and paper to help you. This code is easier to understand smile.gif . Warning: Spoiler! (Click to show)
Code:
#include <iostream>   
#include <iomanip>    
using namespace std;  

float miles, cost, variableShippingCost; 

int main() 
{
        cout << "Enter the distance in miles:" << " "; 
        cin >> miles;    

        if (miles <= 0)  
        {  
                cout << "ERROR: The distance should be a positive value." << endl; 
                cout << endl;    
                system("pause"); 
        }  

            void calculateVariableCost();  
                calculateVariableCost();       

            cout << "The cost of shipment over" << " " << ( long) miles << " " << "miles is" << " ";  
                const char poundsign (156); 
                cout.setf(ios::fixed); cout << setprecision(2);  
                cout << poundsign << cost << "." << endl;
                cout << endl;    
            system("pause"); 
} 

void calculateVariableCost() 
{
        if (miles <= 100) 
        {  
                variableShippingCost = 5.50; 
                cost = ((miles * variableShippingCost) + 50);
        } 

        if ((miles > 100) && (miles <= 500)) 
        {  
                variableShippingCost = 4.00; 
                cost = ((100 * 5.50) + ((miles - 100) * variableShippingCost) + 50); 
        }  

        if (miles > 500) 
        {  
                variableShippingCost = 2.50; 
                cost = ((100 * 5.50) + (400 * 4.00) + ((miles - 500) * variableShippingCost) + 50); 
        }  
}
After you've had a go at both programs I made for you, we can go back to some questions. smile.gif
Edited by BradleyW - 4/21/14 at 5:48pm
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #20 of 32
Test yourself: http://cppquiz.org/ smile.gif
buka
(17 items)
 
  
Reply
buka
(17 items)
 
  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming