I am having Problems compiling and running programs with C++
Hi im janine.
I have been given three programming assignments to be completed this wednesday. I am really in need of help. Please for any assistance.
The 1st program is to
1.) Create a class called DigitSeparator. This class will only have one function called separator. Function separator will have ONE parameter of type long int which will be used to accept a six-digit number. Function separator will take the number passed to the parameter and separate it into its individual digits, multiply each separated digit by 2 and then print the digits separated from one another by three spaces each. (Hint: use a combination of integer division and modulus division to split the number into digits.) Create a driver program that will contain a main function that will create a DigitSeparator object and then call its separator function. Function main should ask the user for a number, and then pass this number to the function separator. For example, if the user types in 423339 then function separator should print
8 4 6 6 6 18
Please note that the user will type ONE integer only that has six digits, not six separate integers. Therefore, your program will only cin>> one data object. You should submit 3 files, DigitSepartor.h, DigitSeparator.cpp and DigitDriver.cpp.
Alrite i did all that but it gives me errors when i try 2 run d program. my code for this program is below:
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "DigitSeparator.h"//include the class definition of DigitSeparator
int main()
{
long int number; //user input
cout<<"Welcome to the Digit Separator Program!\n";//what the user sees on the screen
cout<<"Please enter a six digit number.\n";//prompts user to enter a digit
cin>>number;//input by user is stored in number
DigitSeparator myDigitSeparator;
myDigitSeparator.setSeparator(number);
return 0;
}
Code:
/*Implementations of the DigitSeparator member-function definitions.
The setSeparator function performs validation.
*/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "DigitSeparator.h"//include definition of class DigitSeparator
// class DigitSeparator definition
class DigitSeparator
{
public:
//constructor initializes digits with integer supplied as argument
DigitSeparator(long int number)
{
setSeparator(number)
}//end DigitSeparator constructor
//function to set the digit
void setSeparator(long int number)
{
Separator = number;//store the digit in the object
/*Declaration of variables used for:
Integer Division and Modulus Operations
*/
int num1;
int b;
int num2;
int d;
int num3;
int f;
int num4;
int h;
int num5;
int num6;
//Below is the Integer and Modulus Operations
num1 = Separator/100000;
b = Separator%100000;
num2 = b/10000;
d = b%10000;
num3 = d/1000;
f = d%1000;
num4 = f/100;
h = f%100;
num5 = h/10;
num6= h%10;
//Calculations for outputting final answer
num1 = num1 * 2
num2 = num2 * 2
num3 = num3 * 2
num4 = num4 * 2
num5 = num5 * 2
num6 = num6 * 2
cout<<num1<<" "<<num2<<" "<<num3<<" "<<num4<<" "<<num5<<" "<<num6;
}
}
}; //Ends the DigitSeparator class
Code:
/*DigitSeparator class definition presents the publice interface
of the class. Member-function definitions appear in DigitSeparator.cpp.
*/
//DigitSeparator class definition
class DigitSeparator
{
public:
DigitSeparator();//constructor that initializes a DigitSeparator object
void setSeparator(long int); //function that sets the Separator
};//end class DigitSeparator
The program compiles but when it tries 2 run it states:
The 2nd program is to :
Create a class called NumberFun. This class will contain 6 functions and 1 constructor (a total of 7 functions). It will also have 4 data members of type int called num1, num2, num3 and num4 respectively. Your constructor must have 4 parameters of type int because it will use these to initialize the class’s 4 data members. Your constructor will take the values it receives and pass it to function, setValues. Function setValues will therefore also have 4 parameters to accept the values being passed to it. It will then assign each of those parameters to each data member. The other functions and what they do are described below:
Function sum: will add all the data members and return their sum.
Function average: will compute and return the average of all the data members.
Function product: will compute and return the product of all the data members.
Function smallest: will determine and return data member that has the smallest value.
Function largest: will determine and return data member that has the largest value.
You will now create a driver program that will create a NumberFun object and test all its functions. You should submit a total of 3 files.
Alrite i did that. my code is:
Code:
// main.cpp
#include <iostream>
using std:: cout;
using std:: cin;
using std:: endl;
#include <iostream>
using std:: cout;
using std:: cin;
using std:: endl;
#include "NumberFun.h"
int main()
{
int value1, value2, value3, value4;
cout << "Welcome to the Number Fun Program!" << endl;
cout << "Please enter a number. " << endl;
cin >> value1;
cout << "Please enter a number. " << endl;
cin >> value2;
cout << "Please enter a number. " << endl;
cin >> value3;
cout << "Please enter a number. " << endl;
cin >> value4;
NumberFun myNumberFun(value1 , value2 , value3 , value4);
myNumberFun.calculateSum();
myNumberFun.calculateAverage();
myNumberFun.calculateProduct();
myNumberFun.determineSmallestNumber();
myNumberFun.determineLargestNumber();
return 0;
}
Code:
// NumberFun.cpp
#include <iostream>
using std:: cout;
using std:: cin;
using std:: endl;
#include "NumberFun.h"
NumberFun::NumberFun(int value1 , int value2 , int value3 , int value4)
{
setValues(value1 , value2 , value3 , value4 );
}
void NumberFun::setValues(int value1 , int value2 , int value3 , int value4)
{
{
num1 = value1;
num2 = value2;
num3 = value3;
num4 = value4;
cout << "\nThe number you inputted are: " << endl;
cout << num1 << ", " << num2 << ", " << num3 << ",and " << num4 << endl;
}
}
void NumberFun::calculateSum()
{
{
int sum;
sum = num1 + num2 + num3 + num4;
cout << "\nThe sum of the numbers are: " << sum << endl;
}
}
void NumberFun::calculateAverage()
{
{
int sum = 0;
int average = 0;
sum = num1 + num2 + num3 + num4;
average = sum / 4;
cout << "The average of the numbers are: " << average << endl;
}
}
void NumberFun::calculateProduct()
{
{
int product = 0;
product = num1 * num2 * num3 * num4;
cout << "The product of the numbers are: " << product << endl;
}
}
void NumberFun::determineSmallestNumber()
{
if(num1 < num2)// determine if the first number inputted by user is the smallest number
{
if (num1 < num3)
if (num1 < num4)
cout << "The smallest number is " << num1 << endl;
}
else if (num2 < num1)// determine if the second number inputted by user is the smallest number
{
if (num2 < num3)
if (num2 < num4)
cout << "The smallest number is " << num2 << endl;
}
else if(num3 < num1)// determine if the third number inputted by user is the smallest number
{
if (num3 < num2)
if (num3 < num4)
cout << "The smallest number is " << num3 << endl;
}
else if(num4 < num1)// determine if the third number inputted by user is the smallest number
{
if (num4 < num2)
if (num4 < num3)
cout << "The smallest number is " << num4 << endl;
}
}
void NumberFun::determineLargestNumber()
{
if(num1 > num2)
{
if (num1 > num3)
if (num1 > num4)
cout << "The largest number is " << num1 << endl;
}
else if (num2 > num1)
{
if (num2 > num3)
if (num2 > num4)
cout << "The largest number is " << num2 << endl;
}
else if(num3 > num1)
{
if (num3 > num2)
if (num3 > num4)
cout << "The largest number is " << num3 << endl;
}
else if(num4 > num1)
{
if (num4 > num2)
if (num4 > num3)
cout << "The largest number is " << num4 << endl;
}
}
Code:
// NumberFun.h
#include <iostream>
using std:: cout;
using std:: cin;
using std:: endl;
class NumberFun
{
public:
NumberFun(int , int , int , int);
void setValues(int , int , int , int);
void calculateSum();
void calculateAverage();
void calculateProduct();
void determineSmallestNumber();
void determineLargestNumber();
private:
int num1;
int num2;
int num3;
int num4;
};
it states these following errors when running :
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/NumberFun.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
NumberFun.exe - 2 error(s), 0 warning(s)
my final program is to :
Create a class called Table. Class Table will only have one function called displayTable. Using only the techniques you learned THUS FAR, make function displayTable CALCULATE (and this means you cannot simply hardcode the values in print statements, you must calculate and then print the calculated result) the squares and cubes of the numbers from 0 to 10 and uses tabs to print the following table of values. If you use techniques that were not covered in class, half your points will be deducted. The diagram below illustrates what function displayTable should produce. Create a driver program that tests the Table class.