Error useing a string in more files

Hello there, i have soem problems with my code and i dont know how to fix it, you can help me? Thanks in advance.

There's the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
main.cpp:



#include <iostream>
#include <string>
#include "second.h"

using namespace std;

int main()
{
    string w;
    cin >> w;
    char c = w.at(0);
    cout<<c<<endl;
    eloop();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
second:

#include <iostream>
#include "second.h"

using namespace std;

void eloop()
{
if(c=='a')
{
cout<<"There's an a"<<endl;
}
return;


And i got this error:

F:\Projects\C++\Code Blocks\Mine\PR Encrypt messages\src\econd.cpp|8|error: 'c' was not declared in this scope|

How to tell it to be in ALL?
Thanks in advance!
Last edited on
Hi,

well the compiler said that 'c' was not declared in scope because it wasn't

you may declare char c globally or use a function and point it
Last edited on
Sorry, but i dont understand how to do it, you can give me an example? Thanks in advance
for starters just declare w and c in second: and see if it works
If i get w and c in global in the second one it will auto crash the program, if i call it in local and press anything, after enter it will crash like in the first one, its an long error here.
could you tell what and where at() function is? I don't know about it
Last edited on
closed account (E0p9LyTq)
All you need to do is rewrite your eloop() function:

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include "second.h"

int main()
{
   std::cout << "Enter a string: "; 
   std::string w;
   std::cin >> w;
   char c = w.at(0); // or char c = w[0];
   
   std::cout << c << "\n";

   eloop(c);
}


second.h:
1
2
3
4
5
6
#ifndef __SECOND_H__
#define __SECOND_H__

void eloop(char c);

#endif 


second.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "second.h"

void eloop(char c)
{
   if (c == 'a' || c == 'A')
   {
      std::cout << "The first letter's an a\n";
   }
}


Enter a string: Attack!
A
The first letter's an a
could you tell what and where at() function is? I don't know about it

The at() function, in this case, is a member function of the standard C++ class string. Perhaps you need to study the documentation for this standard C++ class.

could you provide any links?
closed account (E0p9LyTq)
As others suggested you could declare char c globally:

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include "second.h"

char c; // declare c here as a global

int main()
{
   std::cout << "Enter a string: "; 
   std::string w;
   std::cin >> w;
   c = w.at(0);
   
   std::cout << c << "\n";

   eloop();
}


And then tell the compiler the variable can be used by other .cpp files. second.h:
1
2
3
4
5
6
7
8
#ifndef __SECOND_H__
#define __SECOND_H__

void eloop();

extern char c;  // notice the use of extern!

#endif 

http://en.cppreference.com/w/cpp/language/storage_duration

second.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "second.h"

void eloop()
{
   if (c == 'a' || c == 'A')
   {
      std::cout << "The first letter's an a\n";
   }
}
Last edited on
could you provide any links?

Is your "Google" broken?
closed account (E0p9LyTq)
@p007:
http://www.cplusplus.com/reference/string/basic_string/at/

and:

http://en.cppreference.com/w/cpp/string/basic_string/at

BTW, as jlb suggested there is an entire REFERENCE section here at cplusplus, stuffed full of examples:
http://www.cplusplus.com/reference/
Last edited on
thanks :)
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

void eloop(char);

int main()
{
    std::string w = "?";
    std::cin >> w;
    char c = w.at(0);
    std::cout << c << std::endl;
    eloop(c);
}

void eloop(char aChar)
{
    if( aChar =='a' )
        std::cout << "There's an a" << std::endl;
    else
        std::cout << "There's no a" << std::endl;
    return;
}
apple
a
There's an a
banana
b
There's no a
Last edited on
I fixed my problem, thanks so much!
Topic archived. No new replies allowed.