-2
#include <iostream>
using namespace std;

int width;

int getWidth()
{
    int i;
    cin >> i;
    if (5 <= i && i <= 70)
    {
        int width = i;
    }
    else
    {
        cout << "ERROR: The Range Allowed for the Width is 5 to 70, Please try again." << endl;
        getWidth();
    }
    return width;
}

int main() {

    cout << "Welcome to the Flag Generator." << endl;
    cout << "Please enter the Width of your Flag." << endl;

    int getWidth();

    cout << "Your width is: " << width << endl;
}

I'm having problems with my code, this is just a sample of the full code and I am not even receiving a prompt from cin to enter in a width it is just always equal to 0.

I am very new to coding and was hoping to find some answers for it. The output is:

Please enter the Width of your flag.
Your width is: 0

There is not prompt to put in an input even though there is a cin.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Nigel Lum
  • 11
  • 1
  • 3
    Please post code in your question _as text_. See [ask] and take the [tour] for more information. – Paul Sanders Oct 30 '20 at 21:58
  • 1
    Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers. You can edit your question to add the code in the body of your question. For easy formatting use the `{}` button to mark blocks of code, or indent with four spaces for the same effect. The contents of a **screenshot can’t be searched, run as code, or copied and edited to create a solution.** – tadman Oct 30 '20 at 22:00
  • 1
    What do you think `int getWidth();` is going to do in `main`? That's not how a function is called, it's a declaration. – Bob__ Oct 30 '20 at 22:07
  • @Bob__ Would it not repeat that function into main? – Nigel Lum Oct 30 '20 at 22:08
  • @Bob__ How would you call a function then would it be getWidth(width) and swap the "i" with "width" in that function? or would you use a different method? – Nigel Lum Oct 30 '20 at 22:11
  • If you are trying to *call* it to retrieve a value, you should *use* its returned value to initialize a variable: `int value = getWidth();`. It's unclear to me if you are aware that this function is *recursive* (it calls itself) and why aren't you using a loop instead. – Bob__ Oct 30 '20 at 22:12
  • I am fully aware that it calls itself and that I could use a loop instead, though this is a smaller part of an entire code where I am finding it much easier to organize like this due to I must use this type of code 3 times to get a height which is based on the width, and another value based on the height. Thank you for helping me out, I really appreciate your time. – Nigel Lum Oct 30 '20 at 22:16
  • 1
    C++ is an exceptionally hard language to get right through guesswork. Even if you get something that manages to compile, the odds of it producing the desired results approach 0. Here you want to crack open [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read up on variable lifetimes and scope. – user4581301 Oct 30 '20 at 22:29
  • 1
    "Much easier to organize like this" is completely false. This isn't a functional programming language, that has stack implications, and endlessly "iterating" using the stack can be extremely expensive and lead to unnecessary crashes. **Use a loop.** – tadman Oct 30 '20 at 22:33

2 Answers2

2

There's some simple mistakes here likely the result of misunderstanding how C++ works. Here's the corrected code in a more traditional C++ form:

#include <iostream>

// Avoid using namespace std, the std:: prefix serves an important purpose

int getWidth()
{
  // Set up a simple loop that will run forever.
  while (true) {
    std::cout << "Please enter the Width of your Flag." << std::endl;

    int i;
    std::cin >> i;

    if (5 <= i && i <= 70) {
      // Exit condition is met, so exit loop and return valid value
      return i;
    }

    // This branch is where the "didn't validate" path goes
    std::cout << "ERROR: The Range Allowed for the Width is 5 to 70, Please try again." << std::endl;
  }
}

int main() {
  std::cout << "Weclome to the Flag Generator." << std::endl;

  // Declare a local variable that's the result of the getWidth() procedure
  int width = getWidth();

  std::cout << "Your width is: " << width << std::endl;

  return 0;
}

There's several things you need to learn about here:

  • Variable scoping and how int width = i inside a block does not propagate back up to the parent scope.
  • Where function declarations should go, and why int getWidth() inside of main() is not the correct place.
  • How to call functions and, if necessary, capture the return values in variables.
  • How to use loops like while or for to wait for a condition to occur, or to use return or break to break out of loops.
  • Understanding why global variables are problematic and should be avoided, and on where to scope variables more narrowly so as to avoid conflict and confusion.
tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    I expect `int getWidth();` in main was an attempt to call the `getwidth` function not a misplaced declaration. I've seen this confusion between declarations and function calls from newbies before. – john Oct 30 '20 at 22:44
1

Your statement

int getWidth();

doesn't call the getWidth() function. It declares another function named getWidth, returning an int (without defining it). So - nothing touches the width variable before it gets printed.

This aspect of C++ syntax - which is partly inherited from C - can be particularly confusing (even to the more experienced - it just happened to me): Variable declaration, variable initialization/construction, function declaration - can all look very similar. Pay close attention to your textbook, teacher, or working code samples to get this right.

See @tadman's answer for what a corrected program would look like.

einpoklum
  • 118,144
  • 57
  • 340
  • 684