Click here to Skip to main content
15,885,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone. i habe syntax error out below code (this is for practicing purposes so dont care about efficency of code itself):

C++
#include <iostream>
using namespace std;

void funz(char * stringa, int rig, int col)
{char str[5]={'\\','|','/','-','\0'};
 switch(rig,col)
    case(0,0):
        stringa=str[0];
    case(0,col):
        stringa=str[2];    
    case(rig,col):
        stringa=str[0];
    case(rig,0):
        stringa=str[2];
    default:
        stringa=' ';
}

int main()
{   char carat[5][5];
    for(int i=0; i<5;i++)
        {for(int j=0; j<5;j++)
            {funz(carat,i,j);
             cout<<carat[i][j];
            }
        cout<<endl;
        }
 
    return 0;
}


What I have tried:

i tried changing poniter * and comma position and re define variables types.
Thanks for your valuable help in advance.
Posted
Updated 24-Apr-21 9:19am
v2
Comments
[no name] 24-Apr-21 8:28am    
Most probably you will have several syntax errors whith this code and also some serious problems if it finally will compile.
So best is you start with telling us the errors to give us a chance to explain it step by step.
Of course much more better is you read first a tutorial ;)

C++
switch(rig,col)
   case(0,0):

The switch and case statements take a single value, and the case requires it to be a constant value. So you could have something like:
C++
int total = rig * 5 + col;
 switch(total)
    case ???: // you need constant values here. 

So a switch block is not really the optimum choice here, you probably need a set of if/else statements.
 
Share this answer
 
Comments
[no name] 24-Apr-21 8:48am    
A good start at least and a 5 ;) And afterwards the missing break; and assigning values to a pointer like stringa=' ' apart that in case one would use a pointer it will point to a local 'stack variable' which does not survive long.
Richard MacCutchan 24-Apr-21 8:52am    
If you have answers then post a solution. I only addressed one issue.
KarstenK 24-Apr-21 12:58pm    
C++ is supoorting only one parameter in a switch statement.
Richard MacCutchan 24-Apr-21 13:03pm    
That is what I said above.
Quote:
i habe syntax error out below code

When you ask for help, it is a good idea to give complete error massages (which include line number), it help to spot what goes wrong.
C++
switch(rig,col)
   // a '{' is missing here
   case(0,0):
       stringa=str[0];
       // from what you try to do, a 'break' is missing at the end of each case
   case(0,col):
       stringa=str[2];
   case(rig,col):
       stringa=str[0];
   case(rig,0):
       stringa=str[2];
   default:
       stringa=' ';
    // a '}' is missing here

-----
Quote:
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
The position of your '{' on next line is counter intuitive.
C++
#include <iostream>

using namespace std;

void funz(char * stringa, int rig, int col) {
  char str[5] = {
    '\\',
    '|',
    '/',
    '-',
    '\0'
  };
  switch (rig, col)
  case (0, 0):
  stringa = str[0];
  case (0, col):
  stringa = str[2];
  case (rig, col):
  stringa = str[0];
  case (rig, 0):
  stringa = str[2];
  default:
  stringa = ' ';

}

int main() {
  char carat[5][5];
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      funz(carat, i, j);
      cout << carat[i][j];
    }
    cout << endl;
  }

  return 0;
}

Indentation style - Wikipedia[^]
Best C Formatter and Beautifier[^]
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 
Comments
Mat 257 24-Apr-21 10:13am    
hi, i changed the code according to your remarks and it looks like this:

#include <iostream>
using namespace std;

void funz(char * stringa, int rig, int col){
    int R_C=rig + col;
    char str[5]={
        '\\',
        '|',
        '/',
        '-',
        '\0'
        };
 
 switch(R_C){
 case(0):
 stringa=str[0];
 break;
 case(col):
 stringa=str[2];
 break;
 case(R_C):
 stringa=str[0];
 break;
 case(rig):
 stringa=str[2];
 break;
 default:
 stringa=' ';
    }
}


int main(){   
    char carat[5][5];
    for(int i=0; i<5;i++)
    {
        for(int j=0; j<5;j++)
            {
             funz(carat,i,j);
             cout<<carat[i][j];
            }
        cout<<endl;
    }
 
    return 0;
}


other than all errors out wrong swith case syntax (parameters R_C, col and rig are not constant, therefore i ll move to if then statement)
it gives me another error out the pointer to char;

main.cpp: In function ‘void funz(char*, int, int)’:
main.cpp:17:22: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
stringa=str[0];
~~~~~^
main.cpp:20:22: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
stringa=str[2];
~~~~~^
main.cpp:23:22: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
stringa=str[0];
~~~~~^
main.cpp:26:22: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
stringa=str[2];
~~~~~^
main.cpp:29:17: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
stringa=' ';
^~~
main.cpp: In function ‘int main()’:
main.cpp:39:28: error: cannot convert ‘char (*)[5]’ to ‘char*’ for argument ‘1’ to ‘void funz(char*, int, int)’
{funz(carat,i,j);
^
probably i compulsory have to transfer values from stack to heap and recall them from there
thanks
Patrice T 24-Apr-21 10:30am    
Use Improve question to update your question.
Using the suggestion given in the other solution, I provide an implementation that compiles and runs.

C++
#include <iostream>
using namespace std;

void fun( char * array, int size, int row, int col)
{
  char replacement[] = {'\\','|','/','-','\0'};

  int index = size * row + col;

  if ( row == 0)
  {
    if (col == 0)
      array[index] = replacement[0];
    else
      array[index] = replacement[2];
  }
  else
  {
    if (col == 0)
      array[index] = replacement[2];
    else
      array[index] = replacement[0];
  }
}


int main()
{

  char chr[5][5];
  for(int i=0; i<5; i++)
  {
    for(int j=0; j<5;j++)
    {
      fun((char*)chr, 5, i, j);
      cout << chr[i][j];
    }
    cout<<endl;
  }

  return 0;
}

It produces the following output
\////
/\\\\
/\\\\
/\\\\
/\\\\

I don't know if it is the expected output, since I had to infer the requirements from your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900