Problem in scanning scan and storing text in C ++


This is the code of my program:

 #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");
        if ( a == help) 
            printf("\n\nComandos y expresiones de iptables:"); 
            system("iptables %c && iptables -L -v -n", a); 
        return 0; 

The issue is that I can not get either the if or the string variables to work since 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

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

I have tried several times changing nimiedades as instead of using string for the declaration of variables, use char but says the same of the error "string". 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 I take them out it says that the variable h has not been declared.

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

asked by Joaquin Manuel Crespo 10.06.2016 в 20:57

4 answers


Joaquin, you have a good mess of concepts.

1. Inclusions poorly made.

Inclusions require inverted commas or angle brackets:

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


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

The difference between using quotation marks or angle brackets is where the search for the header file begins. Using quotation marks the header will start looking in the folder where the code file is located (extension c or cpp ), using the angle brackets it will start looking in the folder where the language libraries are located.

Forgetting quotation marks or angle brackets will cause failures.

2. C does not have objects string .

The header string.h is a library of that provides a set of functions to work on arrays of characters whose end is marked with the null character ( string ), but does not have object string .

The string object belongs to the , and this is a template that manages strings of characters that do not necessarily have to end with character null, since it internally stores the size.

What's happening?

I suspect that even though you have stuck the wrong inclusions, they are correct on your computer and when you copy-paste your code here it has been de-formatted .

If the problem is not the format of the inclusions, what is the problem? As mentioned in point 2: You have included the header and used an object <iostream> that does not exist (because it does not belong to said library), hence the error arises:

error: ‘string’ was not declared in this scope

Since you have not included the appropriate library, it would be:

#include <string>

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

Most headers in C++ have a version front page at <cmath> where the extension has been removed, the prefix "c" has been added (for example <cstdint> , <cstring> , C++ ) and have been adapted to C++ . If you are working with C you should use the latter not the %code% .


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

answered by 13.06.2016 в 09:53

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 statement is wrong, since a string is part of the variable char, so it is specified and between [] the length of the same. The same happens with the help.

answered by 10.06.2016 в 21:26

1. In C, currently, at the beginning of the main program, the following is available: int main(int argc, char *argv[]) { ... , by default. If in the compiler you use appears by default in another way, leave it as it appears.

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

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

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

5. The system () syntax is not like the printf syntax, so, to process the instruction in the command format, 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");
        //  scanf("%s",a);   // scanf() no admite espacios por defecto, por ello puse gets(), que además está más depurado.
        if (0 == strcmp(a,"-h")) 
            printf("\n\nComandos y expresiones de iptables:"); 
            sprintf(comm_,"iptables %s && iptables -L -v -n",a);
return 1; 

Note: I have tested and verified that the code I suggest in Dev C works. In case your compiler does not have gets (), use scanf () as it is in the comment.

answered by 18.07.2016 в 13:24

If you are programming in C ++, why do not you use the header <iostream> ? that would solve many problems and even make your code more readable

answered by 16.09.2016 в 22:55