-1

I'm not sure why this doesn't work. I'm trying to make a function that passes in an integer and string and returns a string.

string convertThousands(int val, string roman)
{
        piece = (val / THOUSANDS);
        for (int i = 0; i < piece; i++)
        {
            roman += 'M';
        }
        val %= THOUSANDS;
        return roman;
}

in my main function I would call it like below:

string roman;
roman = convertThousands; 
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • 4
    there is no string type in the C standard. is it a type that you defined? share the string type definition if it's the case – MOHAMED Jan 24 '14 at 08:41
  • aditionally: is lopp head declaration of a variable valid in pure C? My teacher told me it isn't, but he isn't that crack, and I'm not that sure. – dhein Jan 24 '14 at 08:42
  • 1
    Your call to `convertThousands()` does not match its prototype. – timrau Jan 24 '14 at 08:42
  • 1
    by not working do you mean it doesn't even compile? Because I will not. – UmNyobe Jan 24 '14 at 08:43
  • 1
    @Zaibis It's allowed in C99. http://stackoverflow.com/questions/1287863/c-for-loop-int-initial-declaration – timrau Jan 24 '14 at 08:44
  • @user3229707 I dont want to sounds like "go read a book", but follow a tutorial on C to understands the synthax and the basic string mechanims – UmNyobe Jan 24 '14 at 08:51
  • @Zaibis It's allowed, since C99. (It wasn't allowed in K&R C nor in the original C standard, but it has been legal for well over 20 years now.) – James Kanze Jan 24 '14 at 09:12
  • @MOHAMED The OP tagged both C and C++. Chances are that this could be (legal) C++. – Stefano Sanfilippo Jan 24 '14 at 12:28
  • I'm pretty sure i'm doing C++, but the course i'm taking is an upgrade of C so I might still be using some c somewhere in my code. – user3229707 Jan 24 '14 at 23:25

5 Answers5

0

First of all you have to call a function in a proper way:

string roman;
int val = 2014;
roman = convertThousands(val, roman); 
Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
0

Your main function should look like this:

string roman;
roman = convertThousands(100, roman); 

Note that 100 should be replaced by whatever value you want to pass to this function.

0

You are not calling the convertThousands method in a proper way. You should do it like this

int value = 2015;
std::string roman;
roman = convertThousands(value, roman);

Inside your method, I don't see anything wrong, so it should run properly.

Victor
  • 13,914
  • 19
  • 78
  • 147
0

Considering the syntactical grammar is correct.

You are still not passing the arguements to the function. How will the function know what is the "val" and "string" that the function will operate on.

main()
{
//caller function
int val = 2000;
string resultstr;
resultstr = convertthousands(val, roman)
}

string convertthousands(int val, string roman)
{
/* called function performs operation on val and roman and can return string after required operation */
}
Adit Ya
  • 731
  • 1
  • 8
  • 19
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char* string;

#define THOUSANDS 1000

string convertThousands(int *val, string roman){
    int piece = (*val / THOUSANDS);
    for (int i = 0; i < piece; i++){
        strcat(roman , "M");
    }
    *val %= THOUSANDS;
    return roman;
}

int main(){
    string roman = malloc(1024);
    strcpy(roman, "");
    int year = 2014;
    roman = convertThousands(&year, roman);
    printf("%d, %s\n", year, roman);
    free(roman);

    return 0;
}

#include <iostream>
#include <string>

using namespace std;

const int THOUSANDS = 1000;

string convertThousands(int &val, string roman){
    int piece = val / THOUSANDS;
    for (int i = 0; i < piece; i++){
        roman += 'M';
    }
    val %= THOUSANDS;
    return roman;
}

int main(){
    string roman("");
    int val = 2014;
    roman = convertThousands(val, roman);
    cout << val << ',' << roman << endl;

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70