switch not assigning value problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huzzaa
    New Member
    • Apr 2007
    • 9

    #1

    switch not assigning value problem

    I am trying to get the user to input 1-10 and that will select a a number from an array and place it into a variable. here is the code.


    Code:
       
       do
        {
          cout << "Enter the first row from column 1 to be summed (1-10)";
          cout << endl << endl;
          cin >> answer1;
          
          switch(answer1)
          {
            case '1': a[0][0] = num1;
              break;
            case '2': a[1][0] = num1;
              break;
            case '3': a[2][0] = num1;
              break;
            case '4': a[3][0] = num1;
              break;
            case '5': a[4][0] = num1;
              break;
            case '6': a[5][0] = num1;
              break;
            case '7': a[6][0] = num1;
              break;
            case '8': a[7][0] = num1;
              break;
            case '9': a[8][0] = num1;
              break;
            case '10': a[9][0] = num1;
              break;
            default : cout << "Invalid input, please select a number 1-10"; 
                      cout << endl << endl;
          }//end switch for answer 1
          
          cout << "Enter the second row from column 1 to be summed (1-10)";
          cout << endl << endl;
          cin >> answer2;
          
          switch(answer2)
          {
            case '1': a[0][0] = num2;
              break;
            case '2': a[1][0] = num2;
              break;
            case '3': a[2][0] = num2;
              break;
            case '4': a[3][0] = num2;
              break;
            case '5': a[4][0] = num2;
              break;
            case '6': a[5][0] = num2;
              break;
            case '7': a[6][0] = num2;
              break;
            case '8': a[7][0] = num2;
              break;
            case '9': a[8][0] = num2;
              break;
            case '10': a[9][0] = num2;
              break;
            default : cout << "Invalid input, please select a number 1-10"; 
                      cout << endl << endl;
          }//end switch for answer 2
       
         num1 + num2 = total;
         total = a[j][1];


    the problem is. when I try and add the two numbers (num1 + num2) it says there is a "non-lvalue in assignment".

    are my switch statements incorect? can I not do that with an array?

    all help is much Appreciated . Thanks in advance.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by huzzaa
    I am trying to get the user to input 1-10 and that will select a a number from an array and place it into a variable. here is the code.


    Code:
       
       do
        {
          cout << "Enter the first row from column 1 to be summed (1-10)";
          cout << endl << endl;
          cin >> answer1;
          
          switch(answer1)
          {
            case '1': a[0][0] = num1;
              break;
            case '2': a[1][0] = num1;
              break;
            case '3': a[2][0] = num1;
              break;
            case '4': a[3][0] = num1;
              break;
            case '5': a[4][0] = num1;
              break;
            case '6': a[5][0] = num1;
              break;
            case '7': a[6][0] = num1;
              break;
            case '8': a[7][0] = num1;
              break;
            case '9': a[8][0] = num1;
              break;
            case '10': a[9][0] = num1;
              break;
            default : cout << "Invalid input, please select a number 1-10"; 
                      cout << endl << endl;
          }//end switch for answer 1
          
          cout << "Enter the second row from column 1 to be summed (1-10)";
          cout << endl << endl;
          cin >> answer2;
          
          switch(answer2)
          {
            case '1': a[0][0] = num2;
              break;
            case '2': a[1][0] = num2;
              break;
            case '3': a[2][0] = num2;
              break;
            case '4': a[3][0] = num2;
              break;
            case '5': a[4][0] = num2;
              break;
            case '6': a[5][0] = num2;
              break;
            case '7': a[6][0] = num2;
              break;
            case '8': a[7][0] = num2;
              break;
            case '9': a[8][0] = num2;
              break;
            case '10': a[9][0] = num2;
              break;
            default : cout << "Invalid input, please select a number 1-10"; 
                      cout << endl << endl;
          }//end switch for answer 2
       
         num1 + num2 = total;
         total = a[j][1];


    the problem is. when I try and add the two numbers (num1 + num2) it says there is a "non-lvalue in assignment".

    are my switch statements incorect? can I not do that with an array?

    all help is much Appreciated . Thanks in advance.
    Not sure what you're trying to do. Do you mean "total = num1 + num2;"? Right after you say "total = a[j][1];". Do you mean "a[j][1] = total"? Could please clarify on what you are trying to do?

    Comment

    • svlsr2000
      Recognized Expert New Member
      • Feb 2007
      • 181

      #3
      Originally posted by huzzaa
      I am trying to get the user to input 1-10 and that will select a a number from an array and place it into a variable. here is the code.


      Code:
       
       
       
       
      num1 + num2 = total;
      total = a[j][1];


      the problem is. when I try and add the two numbers (num1 + num2) it says there is a "non-lvalue in assignment".

      are my switch statements incorect? can I not do that with an array?

      all help is much Appreciated . Thanks in advance.
      num1 + num2 results in rvalue. You cannot assign anything to rvalue. :)
      Are you looking for something like
      total = num1 + num2;

      Comment

      Working...