Problem in taking scanf and storing text in C++

This is my program code:

 #include stdio.h
 #include stdlib.h
 #include string.h

 int main () 
 {
        string a;
        string help = "-h";
        printf("Por favor introduzca la orden. Si desea ver la ayuda introduzca -h");
        scanf(&a);
        if ( a == help) 
        {
            printf("\n\nComandos y expresiones de iptables:"); 
        }
        else 
        {
            system("iptables %c && iptables -L -v -n", a); 
        }
        return 0; 
}

The issue is that I can't get either the if or the string variables to work as gcc tells me this:

TerFirewall.cpp: In function ‘int main()’:

TerFirewall.cpp:5:2: error: ‘string’ was not declared in this scope
  string a;

TerFirewall.cpp:6:9: error: expected ‘;’ before ‘help’
  string help = "-h";


TerFirewall.cpp:8:9: error: ‘a’ was not declared in this scope
  scanf(&a);


TerFirewall.cpp:9:12: error: ‘help’ was not declared in this scope
  if ( a == help) {

I have tried several times changing trifles like instead of using string for variable declaration, using char but it says the same about the "string" error. I also tried trying another way to store the text in the variable string but if I leave single quotes it follows the same error and if you get them saco says that the variable h has not been declared.

And with the if it is something similar because it does not recognize the help. And with scanf() I don't know how to do since I've seen in other forums that it's better than getchar(), but I don't know.

 2
Author: Pako LordPakus, 2016-06-10

4 answers

Joaquin, you have a lot of concepts.

1. Wrong inclusions.

Inclusions require quotes or angular parentheses:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

Or

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

The difference between using quotes or angle brackets is where the header file search is started. Using quotation marks the header will start to search the folder where the code file is located (Extension c or cpp), using the angular parentheses will be it will start searching the folder where the language libraries are located.

Forgetting quotes or angle brackets will cause failures.

2. C does not have string objects.

The string.h header is a library of c that provides a series of functions for working on character arrays whose end is marked with the null character (\0), but does not have an object string.

The object string it belongs to the c++ library, and it is a template that handles strings that do not necessarily have to end with NULL character, since it stores the size internally.

What's going on?

I suspect that although you have pasted the inclusions wrong, they are correct on your computer and by copy-pasting your code here has been unformatted .

If the problem is not the format of the inclusions, what is the problem? So what mentioned in point 2: you have included the header c and used an object string that does not exist (because it does not belong to that library), hence the error:

error: ‘string’ was not declared in this scope

Since you haven't included the proper library, that would be:

#include <string>

Which is a C++ header, not C. C++ headers have no file extension: <iostream>, <vector>, <string>, etc... except headers that exist for C compatibility: <math.h>, <stdlib.h>, <stdint.h>, etc...

Most C headers have a version cover to C++ that has been removed, prefixed with "c" (for example <cmath>, <cstdint>, <cstring>) and have been adapted to C++. If you are working with C++ you should use the latter not those of C.

Conclusion.

You have a mess between the languages you want to use, decide if you want to program in C++ or in C and once you know, use the libraries convenient for it.

 1
Author: PaperBirdMaster, 2020-06-11 10:54:57

Here I put the errors and their corrections.

Error  ----------> Corrección
scanf(&a);          scanf("%c", a);
string a;           char a[800];
string help ="-h";  char help[5] = "help";

The scanf (&a) is wrong in its syntax, since double quotes specify the type of variable to be stored followed by the variable to which it is stored (in this case a). With string something similar happens, since the declaration is erroneous, since a string is part of the Char variable, so it is specified and enter [] the length of it.The same goes for help.

 0
Author: Joaquin Manuel Crespo, 2016-06-11 00:22:06

1. in C, currently, at the beginning of the main program, it is provided: int main(int argc, char *argv[]) { ..., by default. If the compiler you use defaults to otherwise, leave it as it appears.

2. there is no string type as such, but character arrays with string terms '\0'.

3. scanf does not support spaces by default, and it is not really debugged. In C gets () is also available, which is quite better.

4. No you can directly compare two strings, because they are not two variables but two arrays of characters, they are arrays. Then we will use the strcmp() function for it.

5. the syntax of system (), is not like that of printf, so, to process the instruction in the format of the command, we will use sprintf ().

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

int main(int argc, char *argv[]) 
{
        char a[512]="";
        char comm_[570]="";
        printf("Por favor introduzca la orden. Si desea ver la ayuda introduzca -h :\n\n");
        gets(a);
        //  scanf("%s",a);   // scanf() no admite espacios por defecto, por ello puse gets(), que además está más depurado.
        printf("\n");
        if (0 == strcmp(a,"-h")) 
            printf("\n\nComandos y expresiones de iptables:"); 
        else 
        {
            sprintf(comm_,"iptables %s && iptables -L -v -n",a);
            system(comm_); 
        }
return 1; 

note : I have tested and checked that the code I suggest in Dev C works. in case in your compiler you don't have gets (), use scanf() such as it is in the comment.

 0
Author: Sergio X. Liorel Penche, 2016-07-18 11:48:55

If you are programming in C++, Why don't you use the <iostream> header? that would solve many problems and even make your code more readable

 0
Author: Malthael, 2016-09-16 20:55:48