Consider the program hellogen.cpp which is reproduced below. This program produces as output text that is a legal C++ program.
When executed, this program produces as output legal C++ program shown below.
You should modify the program above so that instead of printing Hello World it prompts the user for a name and then writes a program that greets that particular person instead of the world. You should indicate what functions change (e.g., main) and what the changes are.
See the sample output below where the user-entered input is in italics.
prompt% hellogen
enter first name: Oogy
#include <iostream>
using namespace std;
int main()
{
cout << "Hello Oogy" << endl;
return 0;
}
Note: the output of running hellogen is a program. It is a
program that would say hello to name if compiled and executed.
prompt> checkint enter positive int: -3 -3 is not positive, sorry prompt> enter positive int: 15 15 is odd prompt> enter positive int: 24 24 is even
For example, the call printTopTeam("UNC", 8, 1, "Duke", 9, 0) should return "Duke". The call printTopTeam( "FSU", 4, 5, "WFU", 3, 5) should return "FSU". The call printTopTeam("NCSU", 3, 6, "Clemson", 3, 6) should return "TIED".
Complete the function printTopTeam given the header below.
string printTopTeam(string team1, int win1, int loss1,
string team2, int win2, int loss2)
// post: returns which team is better, or "TIED" if teams are tied.
cow
horse
pig
This should also be the output no matter the order in which the same
three strings are passed to the function. Try to write the function so
that it is easy to reason about (being correct).
First compile and run the program numtoword.cpp to figure out the purpose of the program. After running the program you will modify it so that the program works on three and four digit numbers. For extra practice you can make it work for five digit numbers.
Here is one plan for making the program work for three digit numbers. You are welcome to follow a different plan, but this one is simple and requires minimal new programming.
string numToString (int number)
{
if (number < 100)
{
return twoDigitsToString(number);
}
else if (number < 1000)
{
int hundreds = number/100;
string first = digitToString(hundreds);
// fill in code here
}
}
The idea is to break a three digit number into two pieces, the hundreds
and the rest. You can already convert the rest using twoDigitsToString.
You can convert the hundreds part using digitToString as shown
above (note that digitToString's precondition will be satisfied
in the code shown above).enter number between 0 and 99 (inclusive): 56 56 = fifty-six enter number between 0 and 99 (inclusive): 0 0 = zero enter number between 0 and 99 (inclusive): 12 12 = twelve enter number between 0 and 99 (inclusive): 203 Thanks for using our number to English translator.
![]()