C++/C++

Advertisement


Question
QUESTION: Hi, I'm a C++ newbie who has a little trouble with these programs, can you help me with them?
1)
int main() {
int t, count;

for (t=0; t<100; t++) {
  count = 1;
  for (;;) {
   cout << count << ' ';
   count++;
  if (count==10) break; }
  cout << '\n'; }
  return 0; }

The program is suppose to run from 1 to 9, then break and run again, but the program stopped at some point instead of going on forever(the loop is infinite), can you tell me why?

2)
I was asked to write a program that performs until a $ is inputted, here is what i wrote:
int main() {
char c;

do { cout << "Type here: ";
   cin >> c;}
  while (c != $);
return 0;}

but it doesn't work as it should, why?

3)
int main() {
int a;

for (a = 1; a <= 1000; a *= 2) cout << a << " ";
return 0; }

/* if
for (a = 1; a <=100; ) {
a = a * 2;
cout << a << " "; }

would yield 2, 4, 8, 16.... rather than 1, 2, 4, 8, 16....
why? */

I was asked to write a program that demostrate an algebra sequence, 1, 2, 4, 8, 16 ..., I first used the code in the comment line, but it shows 2, 4, 8,.... and then i changed to the code above and run again and got 1, 2, 4, 8,..., can you tell me what is the difference between these? why would i yield difference result?


Additionally, I have some questions about functions:
4) what is the difference between the return type of a function? i noticed that prototype with void as the type can use a cout statement while other can not? what does mean to return or not return a value? what does it mean to return a value?

5) what does it mean: "if a non-void function returns because its closing curly brace is encountered, an undefined value is returned." ?

Thank you so much,
Angela

ANSWER: Hello Angela, thank you for the questions.

I'm sorry for the slight delay. I had to compile your code as I couldn't see anything wrong with it visually.

1) This code should function as you said it should. I copied and pasted it into my compiler and it prints 1-9 one hundred times. I'm not sure why you get an error. Could it be in your compiler settings perhaps? What kind of project did you create? What compiler are you using?

2)

do { cout << "Type here: ";
  cin >> c;}
while (c != $);
return 0;}

This will give a compile error for trying to compare a char variable type to a $. If you wnat to check the value of the char variable, the right hand side of the != has to be in single quotes:

while(c != '$')

That should fix your program error.

3) The reason your commented out code skipped the 1 value when it printed is simple. The first time your for loop executes, a is assigned the value of 1. The fist line in your for loop body modifies the value of a. a = a * 2. So a is assigned the value of 1 * 2, which is 2. Then you print the value of a, so it prints 2.

In the top for loop, the value of a isn't modified until after the code in your for loop has executed. So it prints the current value, then modifies a (a *= 2), then executes again.

4) Returning a value from a function just means that the function computes some sort of value you can use when it is finished executing. Here is a simple example:

int squareNumber(int n)
{
return n * n;
}

This function returns the square of a number passed into it. In your main, you could "catch" the return value as so:

int main()
{
int theValue = squareNumber(5);
cout << theValue; // Prints 25
}

A function that returns void simply doesn't give back a value.

5) This is a bug that can occur if you don't return a value in all possible places in your function.

Take the previous example:

int squareNumber(int n)
{
if(n > 0)
{
return n * n;
}
}

This function now returns the square of a number if it is greater than 0.

// in main
int num = squareNumber(-3);
cout << num; // Will print some random number

Since a negative value was passed into the function, it technically never returns a value. The resulting number is undefined. It should be worth noting that normally compilers will generate a warning for mistakes like that.

I hope this was helpful!

- Eddie

---------- FOLLOW-UP ----------

QUESTION: Thanks, the answers are really clear. I do have a follow-up, however.

Can you show me some other ways to extend the return of a value? I'm trying
to compile a program that needs more than one line to be complete, can you
show me how to return a value that contain a condition or more than one
C++ statements like your example here:
int squareNumber(int n) {
if(n > 0)
{   return n * n;  }
}

Again, thanks so much.

ANSWER: Hello again Angela!

I'm not entirely sure what you mean by extend the return of a value. Do you mean returning different variable types from functions, returning values in conditional statements in functions, etc?

Can you please clarify?

Thanks!

- Eddie

---------- FOLLOW-UP ----------

QUESTION: Well, it doesn't have to be specific type of C++ statements like control
statements, I just need to know how the format would fit, like in your example,
you used a set of braces, are there other possibilities?

Angela

Answer
Hello again Angela!

If I understand your question correctly, you want to be shown how to return values from functions overall.

This depends on what you want the function to do.

int squareNumber(int n)
{
if(n > 0)
{   
return n * n;  
}

char ReturnALetter()
{
char c;
cout << "Enter a letter";
cin >> c;
return c;
}

char ReturnUpperCase()
{
char returnValue = ReturnALetter(); // get the value from the function
if(islower(returnValue))
{
return toupper(returnValue);
}
else
{
// the letter is uppercase already, simply return it
return returnValue;
}

Does this help?

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.