Hi, I need some help from my C++ program. The code that I put below needs to hav
ID: 3882133 • Letter: H
Question
Hi, I need some help from my C++ program. The code that I put below needs to have void main(void) instead of int main() since the requeriments have oid main(void) instead of int main(). It is possible to use main(void)? Because I have problems compiling the code. Thanks.
Code:
#include <iostream>
using namespace std;
struct StringRec
{
int strLen;
char theStr[256];
};
// adds one character to the string
void AddChar(StringRec& str, char theCh);
// adds one character to the string
void OutputString(StringRec str);
// outputs the string and the length of the string
bool CheckString(StringRec str);
// returns true if string is a palindrome, false otherwise
void main(void)
{
StringRec theString;
char theChar;
theString.strLen = 0;
cout << "Enter a string: ";
cin.get(theChar);
while (theChar != ' ')
{
AddChar(theString, theChar);
cin.get(theChar);
}
OutputString(theString);
if (CheckString(theString))
cout << "The string is a palindrome ";
else
cout << "The string is not a palindrome ";
}
// add character one by one in char array and increase strlen by one every time
void AddChar(StringRec& str, char ch)
{
str.theStr[str.strLen] = ch;
str.strLen++;
}
//print element of char array one by one until upto strlen
void OutputString(StringRec str) {
int i;
for (i = 0; i<str.strLen; i++) {
cout << str.theStr[i];
}
}
//print element of char array one by one until upto strlen
// to check string or char array is palindrome or not
//compare the first element with last
//then second with second last......and so on......example.......abcba
// if not follow then break the loop
bool CheckString(StringRec str)
{
int i, f = 0;
for (i = 0; i<str.strLen / 2; i++)
{
if (str.theStr[i] != str.theStr[str.strLen - 1 - i])
{
f = 1;
break;
}
}
if (f == 0)
return true;
else
return false;
}
Explanation / Answer
Dear STudent,
You know that main return a value that is accepted by the operating system for successful or unsuccessful executuion of a program.In C++, main() must return int.
If return value is 0 it means successful, or 1 it means unsuccessful execution.
void main() is legal in C , but is illegal in C++ it specifically requires main to return int for C++.
Since you're using a C++ compiler it would not compiled if you use void main().
==============================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.