Problem in making Global String Array

I am having issue while creating string global.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include<iostream>
#include<conio.h>
#include<string>
using namespace std;
void match ();
void type ();
void dt ();
string LookAhead [6];
	LookAhead [0] = "int";
	LookAhead [1] = "id";
	LookAhead [2] = "[";
	LookAhead [3] = "num";
	LookAhead [4] = "]";
	LookAhead [5] = ";";

int main ()
{
type();	
}

void match (string token)
{
	if (LookAhead[count]== token)
	{
		count++;
	}
	else
	{
		cout<<"error in Match";
	}
}

void type ()
{
	if(lookAhead[count]=="int" || "char")
	{
		dt();
		match('id');
		match('[');
		match('num');
		match(']');
		match(';');
		
	}
}

void dt ()
{
	if (lookAhead[count]=="int")
	{
		match("int");
	}
	else if (lookAhead[count]=="char")
	{
		match("char");	
	}
	else
	{
		cout<<"There is error in Data Type";
	}
}

First of all, why are you making it global?

Anyways, here is how you define a string array - string LookAhead [6] = {"int", "id", "[", "num", "]", ";"};

Also, count you keep using this count thing, but you never create a variable called count, so count is nothing, doesnt exist.
Last edited on
because i have to use this array in all functions.
thanks for the suggestion dear...
I am going to try it..
C:\Users\hp\Desktop\Dev-Cpp\Practise Examples\Untitled1.cpp [Error] 'LookAhead' was not declared in this scope

I am getting this error in type function....


33 13 C:\Users\hp\Desktop\Dev-Cpp\Practise Examples\Untitled1.cpp [Error] invalid conversion from 'int' to 'const char*' [-fpermissive]

nd this in this in type function where i am calling match()/...
Last edited on
There is lots of problems in your code.

Array is spelt with capital L - LookAhead

if(lookAhead[count]=="int" || "char") // that's not a capital L, that's a lowercase L

It's not exactly done like this in c++.

if(lookAhead[count]=="int" || "char")

After the OR-operator ||, you need to type it all over again. "char" is not enough, it has to be

if(lookAhead[count]=="int" || lookAhead[count] == "char")

And lastly, why would you call me dear?


Hi msrt92,

Everyone jumped on the errors. I believe there is something more fundamentally lacking in your programming. The first thing we need to do is chat a second. I see you have the concepts of functions, prototypes, and variables. Good... That's a great thing. You are to be commended on that point. You are well on your way.

Now I want to introduce you to the concept of programming. Something you should have been taught before you ever learned the first command. The key to programming is organization. The more organized you are, the easier you can write your code. The second thing is to agree on a protocol. A protocol, is simply a method... Strings, string functions, global variables (a very bad idea) starting anew... Yes, starting anew is a protocol. Sometimes the best of us say "Ah screw it, and we start over." It doesn't mean you suck, it doesn't mean you've failed. It simply means you have eliminated a process that doesn't work. You've learned what doesn't work, and now you can try something else. Sometimes it's simply the best move. Every programmer here has done it on more than one occasion. I need to honestly say this is the best protocol for you at this time, but it's okay, we're here with you.

Before you begin what I loving refer to as "Hack and slash" coding, You've got to have a plan.
You can't just jump in and start programming, or you'll miss things. Missed things = failed code. Let's make a list of everything this program need in a simplistic form


This program needs an array of type string.
This program needs prototypes.
This program needs to pass the array to every function by reference.
This program needs functions to search, and compare elements to find a match.
This program needs to do this.
This program needs to do that.

Okay... now you are organized.

Step #2 How will we accomplish these goals ?

1. Start with what you KNOW (not think) will work. Test it, test it again, then move on to the next goal.

1
2
3
4
5
6
7
8
9
#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

int main(){
string LookAhead[6] = { "int", "id" , "[" , "num" , "]" , ";" } ;
}


Great, we have an array of strings declared and defined. Okay, that's done.
Now on to the next goal.

We need to write a function.

1
2
3
4
5
6
7
8
9
10
11
12
void type ()
{
	if (LookAhead[counter]=="int" || (LookAhead[counter]=="char")
	{
		dt();
		match("id");
		match("[");
		match("num");
		match("]");
		match(";");
		}
	}


Okay so here we have 5 problems.

1st, count is a keyword, so we need to change this, Let's use 'counter' instead.
2nd, You were calling the functions with chars... eg 'int' and not strings eg "int".
3rd, We don't know the value of counter.
4th, char is not in the string we are comparing to, so it will never match
5th. We need to pass the array into the function by reference...

The first two problems I took care of... How do you suggest we fix the other 3 ?
Topic archived. No new replies allowed.