Dev c++ problem

Hi guys.I just did a program that converts arabic to romanic numbers in Borland c++ and it works fine.Although it may sound a noob question im asking for some help because my code doesnt work in dev c++.All my errors are somethin like this :invalid types `char*[float]' for array subscript

Here is my code:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
float k(char c)
{
switch(c)
{
case'I':
return 1;
case'V':
return 5;
case'X':
return 10;
case'L':
return 50;
case'C':
return 100;
case'D':
return 500;
case'M':
return 1000;
default:
return 0.1;
}
}
void f(char *a)
{ cout<<"a=";gets(a);
float l=strlen(a),x,s=0;
x=l-1;
float t=k(a[x--]);
s=t;
while (x>=0)
{
if(t<=k(a[x]))
s+=k(a[x]);
else
s-=k(a[x]);
t=k(a[x]);
x--;
}
if ((int)(s)!=s)cout<<"eroare";else
cout<<s;
}

int main()
{char a[10];
f(a);
system("pause");
}
The expression specified as a subscript shall be of unscoped enumeration type or of integral type.
The Borland compiler is very old, and both permits and in some cases forces the use of non-standard code.

First, the headers should be:
1
2
3
4
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib> 


Second, your code uses type float to represent integer quantities, and in particular attempts to use a float as an array subscript (or index).
In function f() replace these lines:
1
2
3
4
float l=strlen(a),x,s=0;
x=l-1;
float t=k(a[x--]);
s=t;

with these:
1
2
3
4
5
    int l = strlen(a);
    int x = l - 1;

    float t = k(a[x--]);
    float s = t;


Also, if you use Dev C++ make sure it is the Orwell version as this is kept up to date.
http://orwelldevcpp.blogspot.com/


Ok thanks it works now but now i have another problem.I need to use only 1 function and i have 2 functions..I tried to merge them but unsuccessful.Here is my code

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<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;


float k(char c)
{
	switch(c)
	{
	case'I':
		return 1;
	case'V':
		return 5;
	case'X':
		return 10;
	case'L':
		return 50;
	case'C':
		return 100;
	case'D':
		return 500;
	case'M':
		return 1000;
	default:
		return 0.1;
	}
}
	void f(char *a)
        { cout<<"a=";gets(a);
	int l=strlen(a);
	int x=l-1;
	float t=k(a[x--]);
    float s=t;
	while (x>=0) 
	{
		if(t<=k(a[x]))
			s+=k(a[x]);
		else
			s-=k(a[x]);
		t=k(a[x]);
		x--;     
	}
	 if ((int)(s)!=s)cout<<"error";else
	 cout<<s;
	}

int main()
{ char a[16];
f(a);
system("pause");    	
}







I'm not considering how to make the program work.
But if you really wanted to reduce this to just using one function, you could bring the contents of function f() inside main()

One thing which makes the code very hard to read and understand is the use of single letters as variable names and function names.

There are cases where brevity is useful, for example loop counters or array subscripts may be better kept short, other names, particularly function names are much easier to comprehend if they were meaningful. For example function k() - the body of this function is easy to understand, but the letter k itself reveals nothing about its purpose.

The way I look at it is that the audience of the program code is the human reader. If we were only interested in writing something which the computer could understand, we wouldn't bother with anything other than machine code, most likely in binary.


Topic archived. No new replies allowed.