I am having compiler error. #include <iostream> #include <string> #include \"vow
ID: 3672887 • Letter: I
Question
I am having compiler error.
#include <iostream>
#include <string>
#include "vowelRecursion.h"
#include "intRecursion.h"
using namespace std;
int main()
{
//Variables
int intSizeOfArray = 0,
stringSize = 0,
stringPosition = 0;
string inputString = "";
cout << "Welcome to the Lab04 Main Program" << endl << endl;
cout << "Enter size of int array: ";
cin >> intSizeOfArray;
cout << "Enter each element in the array followed by enter" << endl;
int inputArray[101]; //Creates Int Array
for (int c = 0; c < intSizeOfArray; c++)
{
cout << "> ";
cin >> inputArray[c];
}//for
cout << "The sum of the numbers is " << sumItUp(inputArray, intSizeOfArray) << endl << endl;
//String Vowel Count Section///////////////////////////
cout << "Enter the string to count the vowels in: ";
cin.ignore();
getline(cin, inputString);
cout << "That string has " << vowels(inputString, stringSize, stringPosition) << " vowels in it" << endl;
cout << endl << "Test Functions for both as follows" << endl;
sumItUpTest();
vowelsTest();
return 0;
}
/////////////
#ifndef __Lab04__vowelRecursion__
#define __Lab04__vowelRecursion__
#include <iostream>
using namespace std;
int vowels(string, int, int);
void vowelsTest();
#endif /* defined(__Lab04__vowelRecursion__) */
/////////////
#include <iostream>
#include <string>
#include "vowelRecursion.h"
using namespace std;
int vowels(string inputString, int vowelNum, int position)
{
if (inputString.length() < position)
{
return vowelNum;
}
else
{
if (toupper(inputString[position]) == 'A' || toupper(inputString[position]) == 'E' || toupper(inputString[position]) == 'I' || toupper(inputString[position]) == 'O' || toupper(inputString[position]) == 'U')
{
return vowels(inputString, vowelNum+1, position+1);
}
else
{
return vowels(inputString, vowelNum, position+1);
}
}
}//vowels()
void vowelsTest()
{
string testString = "I have 3";
const int VowelNum = 0;
const int postition = 0;
cout << "That string has " << vowels(testString, VowelNum, postition) << " vowels in it" << endl;
}
/////////
#ifndef __Lab04__intRecursion__
#define __Lab04__intRecursion__
#include <stdio.h>
#include <iostream>
int sumItUp(int [], int);
void sumItUpTest();
#endif
////////////
#include "intRecursion.h"
#include <array>
using namespace std;
int sumItUp(int numArray [], int currentElem)
{
if (currentElem > 0)
{
return (numArray[currentElem] + sumItUp(numArray, currentElem-1));
}
else
{
return numArray[currentElem];
}
}
//sumItUp()
void sumItUpTest()
{
const int sizeOfArray = 8;
int nums[8] = {1,2,3,4,5,6,7,8};
cout << "The sum of the numbers is " << sumItUp(nums, sizeOfArray-1) << endl;
}
Explanation / Answer
I found some of the errors ..
*** In main function you declared size and position as string, where space was missing.
*** In main function .. you have statement "getline(cin, inputString);"
but you didnt included... #include<string.h>
Remaining is fine .. everything is good.
------------------------------------------------------------------------------------------------------------------------------------------------------------
main.h
#include <iostream>
#include <string>
#include "vowelRecursion.h"
#include "intRecursion.h"
using namespace std;
int main()
{
//Variables
int intSizeOfArray = 0,
string Size = 0,
string Position = 0;
string inputString = "";
cout << "Welcome to the Lab04 Main Program" << endl << endl;
cout << "Enter size of int array: ";
cin >> intSizeOfArray;
cout << "Enter each element in the array followed by enter" << endl;
int inputArray[101]; //Creates Int Array
for (int c = 0; c < intSizeOfArray; c++)
{
cout << "> ";
cin >> inputArray[c];
}//for
cout << "The sum of the numbers is " << sumItUp(inputArray, intSizeOfArray) << endl << endl;
//String Vowel Count Section///////////////////////////
cout << "Enter the string to count the vowels in: ";
cin.ignore();
getline(cin, inputString);
cout << "That string has " << vowels(inputString, stringSize, stringPosition) << " vowels in it" << endl;
cout << endl << "Test Functions for both as follows" << endl;
sumItUpTest();
vowelsTest();
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
vowelRecursion.h
#include <iostream>
using namespace std;
int vowels(string, int, int);
void vowelsTest();
/* #endif defined(__Lab04__vowelRecursion__) */
/////////////
#include <iostream>
#include <string>
using namespace std;
int vowels(string inputString, int vowelNum, int position)
{
if (inputString.length() < position)
{
return vowelNum;
}
else
{
if (toupper(inputString[position]) == 'A' || toupper(inputString[position]) == 'E' || toupper(inputString[position]) == 'I' || toupper(inputString[position]) == 'O' || toupper(inputString[position]) == 'U')
{
return vowels(inputString, vowelNum+1, position+1);
}
else
{
return vowels(inputString, vowelNum, position+1);
}
}
}//vowels()
void vowelsTest()
{
string testString = "I have 3";
const int VowelNum = 0;
const int postition = 0;
cout << "That string has " << vowels(testString, VowelNum, postition) << " vowels in it" << endl;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
intRecursion.h
#include <array>
using namespace std;
int sumItUp(int numArray [], int currentElem)
{
if (currentElem > 0)
{
return (numArray[currentElem] + sumItUp(numArray, currentElem-1));
}
else
{
return numArray[currentElem];
}
}
//sumItUp()
void sumItUpTest()
{
const int sizeOfArray = 8;
int nums[8] = {1,2,3,4,5,6,7,8};
cout << "The sum of the numbers is " << sumItUp(nums, sizeOfArray-1) << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.