I am new to coding in C++, and trying to teach myself for class. I need to create a menu driven program that will execute two functions, one to see if a string is a palindrome and the other to see find the greatest common denominator of two numbers.
I have a main.cpp, a GCD.cpp, a palindrome.cpp, a GCD.h and a palindrome.h. When I compile on the command line I get the following error:
/tmp/ccVf007n.o: In Function 'main': main.cpp: (.test+0x75): undefined reference to euclid(int, int); collect2: error: ld returned 1 exit status.
My code blocks are: main.cpp
#include <iostream>
#include "palindrome.h"
#include "GCD.h"
#include <string>
using namespace std;
void showChoices();
int x,y;
int main() {
int choice;
do
{
showChoices();
cin >> choice;
switch (choice)
{
case 1:
cout << "Palindrome Program.";
main();
break;
case 2:
cout << "Greatest Common Denominator Program.";
euclid(x,y);
break;
case 3:
break;
}
}while (choice !=3 );
}
void showChoices(){
cout << "Menu" << endl;
cout << "1. Palindrome Program" << endl;
cout << "2. Greatest Common Denominator Program" << endl;
cout << "3. Exit" << endl;
}
GCD.cpp
#include <iostream>
using namespace std;
int euclid (int*, int*);
int main() {
int a, b;
cout << "A program to find GCD of two given numbers.";
cout << "\n\nEnter your choice of a number: ";
cin >> a;
cout << "\nEnter your choice of another number: ";
cin >> b;
cout << "\n\nProcessing with Euclid method";
cout << "\nThe GCD of " << a << " and " << b << " is " << euclid(a, b);
return 0;
}
int euclid ( int *x, int *y) {
if ( x % y == 0 )
reutrn y;
else return euclid ( y, x%y );
}
palindrome.cpp
#include<iostream>
using namespace std;
int main(){
char string1[20];
int i, length;
int flag = 0;
cout << "Enter a string: ";
cin >> string1;
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
cout << string1 << " is not a palindrome" << endl;
}
else {
cout << string1 << " is a palindrome" << endl;
}
system("pause");
return 0;
}
GCD.h
#ifndef PROJ4_GCD_H
#define PROJ4_GCD_H
int euclid (int, int);
#endif //PROJ4_GCD_H
palindrome.h
#ifndef PROJ4_PALINDROME_H
#define PROJ4_PALINDROME_H
char string1[20];
int i, length;
int flag = 0;
#endif //PROJ4_PALINDROME_H
I appreciate all input and help. Thanks,