a few simple bugs in this code I wrote

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bryan6
    New Member
    • May 2017
    • 4

    a few simple bugs in this code I wrote

    I'm writing a pretty basic c++ program and the same error keeps coming up "char n: redefinition."

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
    char n;

    cout << "Enter a character: ";
    char n = static_cast<int >(n + 1);
    cin >> n;


    cout << n;

    system("pause") ;

    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Notice the following two lines in your code:
    Code:
    char n;
    ...
    char n = static_cast<int>(n + 1);
    Both of these lines declare n as a char. You should only declare it once. Remove "char" from the second line.

    Comment

    • bryan6
      New Member
      • May 2017
      • 4

      #3
      hm, I changed from
      Code:
      char n = static_cast<int>(n + 1);
      to just
      Code:
      n = static_cast<int>(n + 1);
      but the error is just "uninitiali zed local variable 'n' used"?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        What do you expect the value of n to be in the computation of n+1 inside the parentheses?

        Comment

        • bryan6
          New Member
          • May 2017
          • 4

          #5
          Supposed to be a +1 increase in the character int(s) read.

          Comment

          • bryan6
            New Member
            • May 2017
            • 4

            #6
            I found it, just had to trim some fat. Your tip helped though.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Should the cin line go before the static_cast line?

              Comment

              Working...