Translate roman numeral, what am i doing wrong?

Aug 30, 2008 at 2:04pm
Hi, for starters im trying to make this "program" print regular numbers when i input x,v or i. but i am ovbiously doing something wrong, can anyone please tell me what?

i have a feeling it´s somethinh about pointers? thanks a million in advance.


I´ve written the following so far:

#include <iostream>
#include <string>
using namespace std;

string input;

int omregn (string r){
int i;
int nyr[r.length()-1];
for (i=0;i<r.length();++i) {
if (r[i] == ("x"||"X")){
nyr [i] = 10;
}
if (r[i] == ("v"||"V")) {
nyr[i] = 5;
}
if (r[i] == ("i"||"I")) {
nyr[i] = 1;
}

}
return (nyr);
}

int main()
{
cin>>input;
cout<< omregn (input);
}
Aug 30, 2008 at 2:27pm
arrays dont take variable size.they just take constant size.
your int nyr[r.length()-1] is not valid.
u can use vector like: int n=r.length()-1; vector<int>nyr(n);
of course your code has anothers bug too. to use arrays correctly you can read this:
http://www.cplusplus.com/doc/tutorial/arrays.html

Aug 30, 2008 at 3:21pm
I will, thanks
btw is it the same if i use string instead of arrays? i mean strings are practicly arrays arent they?

EDIT: i´ve read it and, sorry, but i still dont think i am sure what i am doing wrong. i mean i could give the lenght to a variable and do something like:

const int thelengthineed = (r.length()-1);
int nyr[thelengthineed];

but then i still get "invalid conversion form int* to int"
Last edited on Aug 30, 2008 at 4:20pm
Aug 30, 2008 at 4:55pm
When you declare an array like that, you're declaring it as static, and static data can't have variable size (this means that, inside the brackets, only literal constants and consts are allowed).
However, there is a way to allocate variable sized arrays. You just dynamically allocate them:
1
2
3
int *arr=new int[len];
//Don't forget to delete them once you're done with them!
delete[] arr;
Aug 30, 2008 at 7:37pm
you should write r[i]=='x' || r[i]=='X' becouse r[i]==('x'||'X') compares char with a bool

int function cant return int[] and I dont understand why do you need that array. Try changing int nyr[r.length()-1] to int nyr and nyr[i]== to nyr+=.
Aug 30, 2008 at 11:15pm
i need to be an array for a part i have not written yet. (i think;-)) i just figured: why write the rest allready when i can´t get what i have so far to work.
i can´t figure it out using string either, so im going to read a tutorial or three more, i´ve read two or three from independent websites so far(not enough i know), none os them said anything about vectors.. or strings. so im going to continue the one on this site.
thanks for the help so far, most likely: i´ll return with some more noob questions
Last edited on Aug 30, 2008 at 11:21pm
Aug 31, 2008 at 4:34pm
@Hamsterman i did what you said and wrote
r[i]=='x' || r[i]=='X'

didn´t exactly solve my problem :-) but why does it only work with: '.
and not with: ".
?
Aug 31, 2008 at 8:12pm
Double quote strings are pointers to static C strings. Single quotes strings can contain just one character and are in fact integral values.
Last edited on Aug 31, 2008 at 8:12pm
Sep 1, 2008 at 6:50pm
Ok. that makes sense, but then why can it compare a string and a integral value. but not a string and a... string?
Sep 1, 2008 at 7:20pm
You can compare a character in a string to another character, but comparing two strings is an entirely different matter:

Let's say we have the string "Hello, World!" and the string "Hello, You!". Strings are not single values, but an arrangement of consecutive values (i.e. an array).
The first string is actually this: {'H','e','l','l','o',',',' ','W','o','r','l','d','!',0}
And the second one is this: {'H','e','l','l','o',',',' ','Y','o','u','!',0}
You can't just compare them. You need to compare their elements individually:
1
2
3
4
5
6
7
8
9
int strcmp(char *a,char *b){
	for (;a && b;a++,b++){
		if (*a>*b)
			return 1;
		if (*a<*b)
			return -1;
	}
	return 0;
}
Last edited on Sep 1, 2008 at 7:20pm
Topic archived. No new replies allowed.