C++ program with comments in main program and each function

Pages: 12
Can someone please help me?

I have to write a complete C++ program, including comments in both the main an din each function, which will do the following:

0. The main program starts by calling a function named introduction which prints out a description of what the program will do. This function is called just once.

This function will not be sent any parameters, and it does not return a value. It will print several lines of output explaining what the program does and how the program and how the program should end.


1. Then the main program asks the user to type in 3 integer values, which could be positive, negative, or zero (see step 4). The main program prints the three integers after they're read in.


2. The main program calls a function named findsmallest, sending it the three integer values. The function will determine the smallest of these three values, sending the answer back to main.

The main program will print the value returned. For example, if you send 4 6 2 to the function, the function will return 2, and the main program will print that 2 is the smallest.

3. The main program will call a function named printmessage, sending it one parameter--the smallest of the integer values. The function will print a message of your choice many times, and the function will not return a value. (BE CAREFUL, SEE BELOW):

For example, if the value of the parameter sent to the function is 2, then the function should print your message 2 times. If the value parameter is 4, the function should print your message 4 times.

However, if the value of the parameter sent to the function is less than or equal to 0, or if the value sent to the function is greater than 10, the function will say it is not possible to print the message.

4. Then the main program will skip a few lines and go back to step 1. At step 1, if the user types in a special combination, the program will go to step 5. You must determine what this combination is and you must explain it--in a prompt and in a comment and in the introduction at the beginning--to the person using the program.


5. At the end, print how many sets of three data values were entered and processed. (Make sure that this number is at least 10.)


YOU WILL BE JUDGED ON THE QUALITY OF YOUR DATA

Type in a total of at least 10 sets of data values.
Have at least one set where all three numbers are equal.
Have the smallest be 1 one time, and have the smallest be 10 one time.
Have at least two sets where the smallest number is either 0 or negative, so that your message is not printed.
Have at least two sets where the smallest number is above 10, so that your message is not printed.
Have at least six sets where you do print your message one or more times.

STYLE: Be sure that each function has a good comment explaining two things: exactly what parameter(s) the function will receive, and exactly what the function will do (and if it returns an answer or prints). For each function, mention the function's parameters by name in the comment.


OUTPUT: Here is some sample output (ignoring the introduction):

the three original integers are -5, 3, -5
the smallest number is -5
it is not possible to print the message in this case

the three original integers are 4, 5 , 3
the smallest number is 3
yourmessage
yourmessage
yourmessage (your message is printed 3 times)
Last edited on
What have you written so far? Make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.
#include <iostream>

using namespace std;

int introduction (int);
int findsmallest (int);
int printmessage (int);

int main ()
{
int a,b,c;

int a =5;
int b = 3;
int c = -1;

cout << ""Enter an integer": ";
cin >> a;

And that's what I have so far
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

You redeclare a, b, and c by accident.
#include <iostream>

using namespace std;
void introduction (int, int, int);
int findsmallest;
int printmessage;

int main()
{
int a,b,c;

a = 4;
b = 3;
c = 0;
cout << "the integers are " << a << " " << b << " "
<< c << endl;

return 0;
}

That's what I have so far
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

What specifically are you stuck with?
How to start to writing the program
Carefully read through the assignment. As you do so, create a sketch of your code. If you don't know exactly how to do something, put comments in describing it. When you get to the end, go back to the beginning to see if you've got everything. Then work on making the code compile and run.

Finally, and this is very important, go back through the assignment and your code again to make sure you have everything.

I'll help you get started with the first few points:

0. The main program starts by calling a function named introduction which prints out a description of what the program will do. This function is called just once.

This function will not be sent any parameters, and it does not return a value. It will print several lines of output explaining what the program does and how the program and how the program should end.


1
2
3
4
5
6
7
8
9
void introduction()
{
    // print out a description of the program
}

int main()
{
    introduction();
}


1. Then the main program asks the user to type in 3 integer values, which could be positive, negative, or zero (see step 4). The main program prints the three integers after they're read in.

Okay, so now main is:
1
2
3
4
5
6
7
8
9
10
int main()
{
    introduction();

   // prompt for 3 integers and print them out
   int a, b, c;
   cout << "Enter 3 numbers: ";
   cin >> a >> b >> c;
   cout << "You entered " << a << " " << b << " " << c << '\n';
}


2. The main program calls a function named findsmallest, sending it the three integer values. The function will determine the smallest of these three values, sending the answer back to main.

So the prototype for findsmallest is like this:
1
2
3
4
int findsmallest(int a, int b, int c)
{
    // return the smallest value of a, b, and c.
}

and you call it in main to find the smallest value:
int smallest = findsmallest(a, b, c);

The main program will print the value returned. For example, if you send 4 6 2 to the function, the function will return 2, and the main program will print that 2 is the smallest.

Okay, so after you get the smallest, you print it out:
1
2
   int smallest = findsmallest(a, b, c);
   cout << "The smallest number is " << smallest << '\n';


etc.
This helped me out a lot. Thank you very much for your help
Here is an if else and its equivalent, it may help.

Run each and see

1st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>


using std::cout;
using std::cin;

int main()
{
	//arbitrary vals
	double a{};
	double b{};
	double c{};

	cout << "enter 2 numbers: " << '\n';

	cin >> b >> c;

	cout << "\n\ta = " << a << "\tb = " << b << "\tc = " << c << '\n';

	//if else 
	if (b > c)
	{
		 a = b - c;
	}
	else
	{
		 a = 0;
	}

	cout << "\ta = " << a << '\n';

	return 0;

}


2nd equivalent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>


using std::cout;
using std::cin;

int main()
{
	//arbitrary vals
	double a{};
	double b{};
	double c{};

	cout << "enter 2 numbers: " << '\n';

	cin >> b >> c;

	cout << "\n\ta = " << a << "\tb = " << b << "\tc = " << c << '\n';

	//This is equivalent to the if else, bit less cumbersome 
	a = b > c ? b - c : 0;

	cout << "\ta = " << a << '\n';

	return 0;

}


Hopefully this doesn't confuse.

Step through with F10 in visstud to see things more clearly.
You can learn a lot more using vis stud rather than, say, code::blocks.
Last edited on
I started the same program but a different way but I'm stuck

//This program will read in three integers
#include <iostream>

void introduction (void);
int findsmallest (int, int, int);
int printmessage (int);
using namespace std;

void introduction(void)
{
//Print out a program to find the smallest integer
}

int main()
{
introduction();

//Prompt for 3 integers and print them out
int a, b, c;
cout << "Enter 3 integers: ";
cin >> a >> b >> c;
cout << "I entered " << a << " " << b << " " << c << endl;
}


int findsmallest (int a, int b, int c);
{
findsmallest();

//Return the smallest value of a, b, c
int findsmallest (a, b, c);
cout << "The smallest numbers is " << smallest << endl;
}

int printmessage (int)
{

}
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

What specifically are you stuck with?
What do you mean "code is [code] between code tags...? I am very new to C++
I'm stuck what to write for int printmessage (int)
It has nothing to do with C++ ;) http://www.cplusplus.com/articles/z13hAqkS/
I don't use "code is ...." in class so I see what you're saying now, after reading the article, but I need help and that's not gonna help me
Since you're too lazy to type 13 characters to make it easier for me to help you, I spent some of my own time to do it for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//This program will read in three integers
#include <iostream>

void introduction (void);
int findsmallest (int, int, int);
int printmessage (int);
using namespace std;

void introduction(void)
{
    //Print out a program to find the smallest integer
}

int main()
{
    introduction();

    //Prompt for 3 integers and print them out
    int a, b, c;
    cout << "Enter 3 integers: ";
    cin >> a >> b >> c;
    cout << "I entered " << a << " " << b << " " << c << endl;
}


int findsmallest (int a, int b, int c);
{
    findsmallest();

    //Return the smallest value of a, b, c
    int findsmallest (a, b, c);
    cout << "The smallest numbers is " << smallest << endl;
}

int printmessage (int)
{

}
Please at least take the time answer the question: what specifically are you stuck with?
Steps 4 and 5
4 wrote:
Then the main program will skip a few lines and go back to step 1. At step 1, if the user types in a special combination, the program will go to step 5. You must determine what this combination is and you must explain it--in a prompt and in a comment and in the introduction at the beginning--to the person using the program.
Which part specifically? You're being extremely evasive in asking for help.
Actually step 4, not step 5. I've asked the professor what special combination is she referring to and she wouldn't answer my question
Your professor must watch a lot of anime with his special combination bullshit. Any special reason he wouldnt tell you what this magical spell is?

Edit: It does say
You must determine what this combination is and you must explain it


But What does "special combination" even mean?
Last edited on
Pages: 12