I am having a problem with my program. We are supposed to ask for two numbers as input then half the larger and double the smaller. If the larger is odd then the smaller is added to an accumulator.

My problem lies (i'm guessing) somewhere in the functions.

Here is my code and below is my compile error.

/* Program Name:multiplication.cpp

   Description: Program does reverse multiplication

   Name:Scott Streit			Class No.: 4467
   Date:11/09/09			 	VisualC++
*/
//********************************** Includes
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <sstream>

#define cls system("cls")
#define frz system("pause");
#define yl  system("color 0e");

using namespace std;

//********************************** Type definitions
ofstream print("i:\\c++\\multiplication.txt");
//********************************** Function Prototypes
void input(int &nnum1, int &nnum2);
void findbig(int nnum1,int nnum2, int &bbig,int &ssmall);
void math(int bbig, int ssmall, int &ttotal);
void display();
//********************************** Main Function


int main()
{
	time_t t;
	time(&t);
	yl;
	cls;
	int num1, num2, big, small;
	int total=0;
		
	input(num1, num2);
	findbig(num1, num2, big, small);
	math(big, small, total);
	display();

      
	frz;
	return 0;
} // end of main function

//********************************** Function Definitions
void input(int &nnum1, int &nnum2)
{
	cout<<"Please enter your first number: ";
	cin>>nnum1;
	cout<<"Please enter your second number: ";
	cin>>nnum2;
}//end input

void findbig(int  nnum1, int nnum2,int bbig, int ssmall)
{
	if(nnum1>nnum2)
	{
		bbig=nnum1;
		ssmall=nnum2;
	}
	else
	{
		bbig=nnum2;
		ssmall=nnum1;
	}
	cout<<bbig<<"\n";
}//end findbig


void math(int bbig, int ssmall, int ttotal)
{
	ttotal=0;
	
	while (bbig!=0)
	{
		if (bbig%2==0)
		{
			(bbig/2);
			(ssmall*2);
		}//end if
		else
		{
			ttotal+=ssmall;
			(bbig/(2));
			(ssmall*(2));
		}//end else
	}//end while
			
	cout<<(bbig*ssmall)<<"\n";
	cout<<ttotal<<"\n";
}//end math

void display()
{
	cout<<"at display\n";
}//end display

Here is the compiler error.
1>------ Build started: Project: multiplication, Configuration: Debug Win32 ------
1>Compiling...
1>multiplication.cpp
1>i:\c++\multiplication\multiplication\multiplication.cpp(45) : error C2062: type 'char' unexpected
1>i:\c++\multiplication\multiplication\multiplication.cpp(49) : error C2062: type 'char' unexpected
1>i:\c++\multiplication\multiplication\multiplication.cpp(50) : error C2062: type 'char' unexpected
1>Build log was saved at "file://i:\c++\multiplication\multiplication\Debug\BuildLog.htm"
1>multiplication - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Dani AI

Generated

Two distinct problems are visible in the thread and they should be fixed separately.

First, the C2062 (“type 'char' unexpected”) was caused by a macro collision: a macro like #define small char makes int small; become int char; which is invalid. spotted that. You already moved to smallnum/bignum, which is one safe fix. Other options are to #undef small immediately after the header that defines it, or avoid pulling in the header that introduces that macro. Renaming the variable is the simplest and safest approach.

Second, the LNK2019 unresolved externals come from mismatched declarations vs. definitions. As said, your prototypes must match the function signatures exactly (including & and const qualifiers). In your case the prototypes declare reference parameters but the function definitions do not. Make the definitions match the prototypes (for example, void findbig(int, int, int &bbig, int &ssmall) and void math(int bbig, int ssmall, int &ttotal)), or change the prototypes to match the definitions. The linker error is just the compiler telling you the mangled names differ.

Also fix the logic in math: lines like (bbig/2); and (ssmall*2); compute values and discard them — use assignments (bbig /= 2; ssmall *= 2;). Ensure ttotal is actually modified in math (pass by reference or return the value). Use while (bbig > 0) (or explicitly handle negatives) so the loop terminates.

Quick checklist: rename or undefine conflicting macros; make declaration/definition signatures identical; replace no-op expressions with assignment operators; update or return the accumulator. After those changes the compile and link errors should disappear and the algorithm will compute the product correctly.

Recommended Answers

All 3 Replies

The problem is #define small char . You can't do this: int char , which is what you are doing then you say int small;

ok fixed one problem and created another.

here is my new error.

1>multiplication.obj : error LNK2019: unresolved external symbol "void __cdecl math(int,int,int &)" (?math@@YAXHHAAH@Z) referenced in function _main
1>multiplication.obj : error LNK2019: unresolved external symbol "void __cdecl findbig(int,int,int &,int &)" (?findbig@@YAXHHAAH0@Z) referenced in function _main
1>I:\c++\multiplication\Debug\multiplication.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://i:\c++\multiplication\multiplication\Debug\BuildLog.htm"
1>multiplication - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

//********************************** Includes
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <sstream>

#define cls system("cls")
#define frz system("pause");
#define yl  system("color 0e");

using namespace std;

//********************************** Type definitions
ofstream print("i:\\c++\\multiplication.txt");
//********************************** Function Prototypes
void input(int &nnum1, int &nnum2);
void findbig(int nnum1,int nnum2, int &bbignum,int &ssmallnum);
void math(int bbig, int ssmall, int &ttotal);
void display();
//********************************** Main Function


int main()
{
	time_t t;
	time(&t);
	yl;
	cls;
	int num1, num2, bignum, smallnum;
	int total=0;
		
	input(num1, num2);
	findbig(num1, num2, bignum, smallnum);
	math(bignum, smallnum, total);
	display();

      
	frz;
	return 0;
} // end of main function

//********************************** Function Definitions
void input(int &nnum1, int &nnum2)
{
	cout<<"Please enter your first number: ";
	cin>>nnum1;
	cout<<"Please enter your second number: ";
	cin>>nnum2;
}//end input

void findbig(int  nnum1, int nnum2,int bbignum, int ssmallnum)
{
	if(nnum1>nnum2)
	{
		bbignum=nnum1;
		ssmallnum=nnum2;
	}
	else
	{
		bbignum=nnum2;
		ssmallnum=nnum1;
	}
	cout<<bbignum<<"\n";
}//end findbig


void math(int bbignum, int ssmallnum, int ttotal)
{
	ttotal=0;
	
	while (bbignum!=0)
	{
		if (bbignum%2==0)
		{
			(bbignum/2);
			(ssmallnum*2);
		}//end if
		else
		{
			ttotal+=ssmallnum;
			(bbignum/(2));
			(ssmallnum*(2));
		}//end else
	}//end while
			
	cout<<(bbignum*ssmallnum)<<"\n";
	cout<<ttotal<<"\n";
}//end math

void display()
{
	cout<<"at display\n";
}//end display

Match your prototypes with function signatures.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.