Reading characters from a string and assigning them to a single char???
Hi there,
I'm having a bit of a mare.
I am attempting to convert ROMAN NUMERALS into ARABIC NUMBERS...
I cannot for the life of me seem to be able to read a character from a string...
I have been trying to write a 'SORT' function that uses a switch to sort through and count the different letters in a string that has been input either via the keyboard or from a file stream...
Here is my full programme so far... any hints as to how I could read the letters would be a godsend... I have been doing this for about 20 huors straight and still can't get it!
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char LETTERS[16];
int index;
int REP;
int fork;
int countM, countD, countC, countL, countX, countV, countI;
int countCM, countCD, countXC, countXL, countIX, countIV;
int valueM; int valueD; int valueC ; int valueL; int valueX;
int valueV; int valueI; int valueCM; int valueCD; int valueXC;
int valueXL; int valueIX; int valueIV;
void READFILE ();
void READKEYBOARD ();
void SORT (char[]);
void RESTART ();
void COUNT (int);
void COUNTLESS (int);
void COUNTER (char []);
void READLETTER2 (char[]);
int TOTAL (int, int, int, int, int, int, int, int, int, int, int, int, int);
int TOTAL (int m, int d, int c, int l, int x, int v, int i, int cm, int cd, int xc, int xl, int ix, int iv) // TOTALS UP THE VALUES OF THE NUMERALS
{
return (m+d+c+l+x+v+i+cm+cd+xc+xl+ix+iv);
}
void COUNTER (char counter[16]) // TALLIES AND ERROR CHECKS THE DOUBLE NUMERAL COUNTS
{
switch (counter[index])
{
case '\0':
break;
case 'D':
REP = 1; COUNTLESS(countC); COUNT(countCM); break;
case 'M':
REP = 1; COUNTLESS(countC); COUNT(countCD); break;
case 'C':
REP = 1; COUNTLESS(countX); COUNT(countXC); break;
case 'L':
REP = 1; COUNTLESS(countX); COUNT(countXL); break;
case 'X':
REP = 1; COUNTLESS(countI); COUNT(countIX); break;
case 'V':
REP = 1; COUNTLESS(countI); COUNT(countIV); break;
default:
printf("You have input an invalid character");
RESTART ();
}
}
void COUNTLESS (int countless) // CHECKS AND TALLIES THE SINGLE NUMERAL COUNTS
{
(countless --);
}
void RESTART () // RESTARTS THE WHOLE PROGRAMME
{
int yn;
printf("Do you wish to try again? Enter Y to continue \n(else enter anything else to quit): ");
scanf("%i", &yn);
if ((yn = 'Y')|| (yn = 'y'))
printf("Please quit and restart the programme");
else
exit(0);
}
void SORT (char UNKNOWN[]) // DETERMINES WHICH NUMERALS HAVE BEEN ENTERED FROM THE STRING AND PERFORMS FUNCTION "COUNT" ON THEM
{
switch (UNKNOWN[index])
{
case 'M':
REP = 4; COUNT(countM); break;
case 'D':
REP = 1; COUNT(countD); break;
case 'C':
REP = 3; COUNT(countC); READLETTER2(LETTERS); break;
case 'L':
REP = 1; COUNT(countL); break;
case 'X':
REP = 3; COUNT(countX); READLETTER2(LETTERS); break;
case 'V':
REP = 1; COUNT(countV); break;
case 'I':
REP = 3; COUNT(countI); READLETTER2(LETTERS); break;
default:
printf("You have input an invalid character"); RESTART ();
}
}
void READLETTER2 (char UNKNOWN[]) // READS THE SECOND THE SECOND NUMERAL AFTER THE PRIMARIES C, X and I AND PERFORMS TALL
{
index ++;
COUNTER (LETTERS);
}
void COUNT (int count) // CHECKS AND TALLIES THE SINGLE NUMERAL COUNTS
{
(count ++);
if (count > REP)
{
printf("You have input too many consecutive numerals");
RESTART();
}
}
void READKEYBOARD () // FOR KEYBOARD MODE ASK FOR NUMERALS AND COPY INTO "LETTERS"
{ //get the user to input a ROMAN NUMERAL SEQUENCE
char Tab[16];
printf("\n Please input your ROMAN NUMERALS from the keyboard (in CAPS only): ");
scanf("%s", &Tab);
strcpy(LETTERS, Tab);
}
void main ()
{ // DECLARE VARIABLES ETC...
int ANSWER;
int c=0;
int countM = 0; int countD = 0; int countC = 0; int countL = 0; int countX = 0; int countV = 0; int countI = 0;
int countCM = 0; int countCD = 0; int countXC = 0; int countXL = 0; int countIX = 0; int countIV = 0;
// CHOOSE BETWEEN KEYBOARD & BATCH MODE
do
{
printf("\n Choose 1 for FILE input");
printf("\n Choose 2 for KEYBOARD input: ");
scanf("%i", &c);
}
while ((c <=0) || (c >=3));
if (c == 1)
printf("You have chosen one - file input");
else if (c == 2)
{
printf("You have chosen two - file input"); // FOR KEYBOARD MODE ASK FOR NUMERALS AND COPY INTO "LETTERS"
READKEYBOARD ();
}
for (index = 0; LETTERS[index] != '\0'; index++);
SORT (LETTERS);
index = -1;
int valueM = 1000*countM; int valueD = 500*countD; int valueC = 100*countC; int valueL = 50*countL; int valueX = 10*countX;
int valueV = 5*countV; int valueI = 1*countI; int valueCM = 900*countCM; int valueCD = 400*countCD; int valueXC = 90*countXC;
int valueXL = 40*countXL; int valueIX = 9*countIX; int valueIV = 1*countIV;
ANSWER = TOTAL(valueM, valueD, valueC, valueL, valueX, valueV, valueI, valueCM, valueCD, valueXC, valueXL, valueIX, valueIV);
printf("The value you input in ROMAN NUMERALS was: %s", LETTERS);
printf("The equivalent value in Arabic numbers is: %i", ANSWER);
}
Passing more than one variable through an array
Hello again,
I have had a go at piecing this back together into a more orderly structure and have learnt a few things from your advice...
I am just a little bit confused about how to use these counts properly. I thought if I used them as a global variable it would work but aren't there too many of them to keep passing through functions and how would I go about doing that. I can't see how it would be done. I think I have corrected the index... passing? I mean to pass the counts between 'sort' 'readletter' and 'counter' functions... seems impossible to my small brain.
Any advice would be taken humbly...
Thanks
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char LETTERS[16];
int total (int, int, int, int, int, int, int, int, int, int, int, int, int);
int countless (int);
int counter (char, int);
int readletter2 (char, int);
int count (int, int);
void sort (char); // converts the NUMERALS into counts
void readkeyboard (); // reads the string from the keyboard
void ask () ; // asks for input type
int fork(int); // choose (F)ile or (K)eyboard
int total (int m, int d, int c, int l, int x, int v, int i, int cm, int cd, int xc, int xl, int ix, int iv)
{
return (m*1000) + (d*500) + (c*100) + (1*50) + (x*10) + (v*5) + (i) + (cm*900) + (cd*400) + (xc*90) + (xl*40) + (ix*9) + (iv*4);
}
int countless (int a)
{
a --;
return a;
}
int counter (char Tab[16], int i) // TALLIES AND ERROR CHECKS THE DOUBLE NUMERAL COUNTS
{
switch (Tab[i])
{
case '\0':
break;
case 'D':
REP = 1; countless(countC); count(countCM, REP); break;
case 'M':
REP = 1; countless(countC); count(countCD, REP); break;
case 'C':
REP = 1; countless(countX); count(countXC, REP); break;
case 'L':
REP = 1; countless(countX); count(countXL, REP); break;
case 'X':
REP = 1; countless(countI); count(countIX, REP); break;
case 'V':
REP = 1; countless(countI); count(countIV, REP); break;
default:
printf("You have input an invalid character");
RESTART ();
}
return i;
}
int readletter2 (char Tab[16], int i) // READS THE SECOND THE SECOND NUMERAL AFTER THE PRIMARIES C, X and I AND PERFORMS TALL
{
i ++;
counter (Tab[16], int i);
return i;
}
int count (int i, int rep) // CHECKS AND TALLIES THE SINGLE NUMERAL COUNTS
{
(i ++);
if (i > rep)
{
printf("You have input too many consecutive numerals");
RESTART();
}
return i;
}
char sort (char Tab[16])
{
int i;
int REP;
for (i = 0; Tab[i] != '\0'; i++);
{
switch (Tab[i])
{
case 'M':
REP = 4; count(countM, REP); break;
case 'D':
REP = 1; count(countD, REP); break;
case 'C':
REP = 3; count(countC, REP); readletter2(Tab[i], i); break;
case 'L':
REP = 1; count(countL, REP); break;
case 'X':
REP = 3; count(countX, REP); readletter2(Tab[i], i); break;
case 'V':
REP = 1; count(countV, REP); break;
case 'I':
REP = 3; count(countI, REP); readletter2(Tab[i], i); break;
default:
printf("You have input an invalid character"); RESTART ();
}
}
}
void readkeyboard (char Tab[16]) // FOR KEYBOARD MODE ASK FOR NUMERALS AND COPY INTO "LETTERS"
{ //and change string to upper case
int x;
printf("\n Please input your ROMAN NUMERALS from the keyboard: ");
scanf("%s", LETTERS);
for (x=0; x<strlen(LETTERS); ++x)
{
LETTERS[x] = toupper(LETTERS[x]);
}
}
void ask ()
{
printf("\n Choose 1 for FILE input");
printf("\n Choose 2 for KEYBOARD input: ");
}
int fork(int c)
{
scanf("%i", &c)
while ((c <=0) || (c >=3));
if (c == 1)
printf("You have chosen one - file input");
else if (c == 2)
{
printf("You have chosen two - file input"); // FOR KEYBOARD MODE ASK FOR NUMERALS AND COPY INTO "LETTERS"
readkeyboard ();
}
int main ()
{ // DECLARE VARIABLES ETC...
int countM = countD = countC = countL = countX = countV = countI = countCM = countCD = countXC = countXL = countIX = countIV = 0;
int answer = 0;
// CHOOSE BETWEEN KEYBOARD & BATCH MODE
ask();
fork ();
sort (LETTERS);
answer = total(countM, countD, countC, countL, countX, countV, countI, countCM, countCD, countXC, countXL, countIX, countIV);
printf("The value you input in ROMAN NUMERALS was: %s", LETTERS);
printf("The equivalent value in Arabic numbers is: %i", answer);
}