Author Topic: C++ help needed.  (Read 932 times)

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
C++ help needed.
« on: July 02, 2005, 10:51:56 pm »
Sigh... Here is the program:

Code: [Select]

/* Number Identifyer

Char :: ASCII
1-9  :: 48-57
A-Z  :: 65-90
 e   :: 101
 ^   :: 94
 .   :: 46

*/

#include
using namespace std;



void spcRmv(char line[30]){ //Removes spaces.
   int a=0;
    int i=0;
    for(;i       if(line[i] != 32) line[a++] = line[i];

  for(;a       line[a]=0;

}


int chkTp(char line[30]){    // Checks type
  int a=0;
    int i=0;

    if(line[a] != 0){
       for(;i           if(line[i] == \'.\') return 1; // Return \"1\" if line is fractional decimal.
           if(line[strlen(line)]) return 2; // Return \"2\" if line is decimal.
    }}
  else return 3; //!!!!!!!!!!!!!TO BE EXPANDED ON!!!!!!!!!!!!!
    }



void cnvrtDec(char line[30]){
  int i=0;
    int n1; //Used to hold upcoming change.
 int n2=0; //Used to hold curent value;
  int x=1; //Used for conversion.

 for(;i       n1 = line[i] - 48;
      n2 = (n2 * x) + n1;
 }

   cout << \"\\n\\nValue Entered: \" << line;
  cout << \"\\nValue Type: Decimal\";
   cout << \"\\nDecimal Value: \" << n2;
 cout << \"\\n\\n\";
}






int main()
{
 char impLine[30];   //For imput.

    for(;;){ //Controls entire program.

     for(int chk;;){ // Input value.
         cout << \"Input value: \";
          gets(impLine);
     


            for(int i=0; i                if((48 <= impLine[i] <= 57) || (65 <= impLine[i] <= 90) || (impLine[i] == 101 || 94 || 46 || 32));
             else { chk=1; break;} // If wrong value is given.
           }
   
            if(chk == 1){cout << \"\\nAlowed values: 1-9, e, ^, A-Z & .\\n\\n\"; continue;}
           else;

           if(strlen(impLine)<29) break; // Makes sure string size is 30 or less characters.
           cout << \"\\nNo more than 30 characters!\\n\\n\";
     }

       cout << \"\\n\\n\";

     spcRmv(impLine);

        if(chkTp(impLine) == 1) continue; //Imput value identified as fraction (!!!NOT YET SUPORTED!!!)
     else if(chkTp(impLine) == 2) cnvrtDec(impLine); //Imput value identified as decimal, and shall be converted.
        else continue; //No more values yet suported!!!
 }

   return 0;
}


All it does it take the value that you imput, and tells you about it (What you entered, Type of value, Equivalent Decimal value).

Eventualy I\'m trying to get it to suport Decimals, Octals, Hex, Fractions, and Exponentials. But as of now the program is bearly breathing and I don\'t know why... Help?
« Last Edit: July 02, 2005, 10:52:17 pm by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

Keyaz

  • Guest
(No subject)
« Reply #1 on: July 02, 2005, 11:56:34 pm »
you need to put a link in there to some naked pictures of some hot girls :|

on the more honest side i have no idea :)

Entamis

  • Hydlaa Resident
  • *
  • Posts: 153
    • View Profile
(No subject)
« Reply #2 on: July 03, 2005, 01:54:33 am »
You can\'t do 2 comparisons at a time like that:
Code: [Select]
(48 <= impLine[i] <= 57)
You should instead write:
Code: [Select]
(48 <= impLine[i]) && (impLine[i] <= 57)
In the same manner:
Code: [Select]
(impLine[i] == 101 || 94 || 46 || 32)
should be written as:
Code: [Select]
(impLine[i] == 101) || (impLine[i] == 94) || (impLine[i] == 46) || (impLine[i] == 32)
There are more errors but I\'m going to sleep now.. I\'ll help you tomorrow. It\'s a small program so it won\'t hurt if you rewrite it completely. I suggest that you begin with the most basic functionality and don\'t add more until you checked it works. You can also place cout\'s all around the program to check where exactly it fails (or use a debugger if you know how).

EDIT: Oh well, I don\'t have time today either. I\'ll do it soon(TM).
« Last Edit: July 04, 2005, 01:37:32 am by Entamis »

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #3 on: July 04, 2005, 11:43:33 am »
Uh jesus another one of these debug my c++ threads.  Id rather just write my own program, but maybe that wont help you much,

Anyway a few quesitons:
Firstly how is the program going to tell the difference between an octal value and a decimal value? It certaintly cant work it out- e.g the Number 5647 could be Octal or it could be decimal.  It could even be Hex.  Im guessing you\'re going to need the user to tell them before hand what it is...

Entamis

  • Hydlaa Resident
  • *
  • Posts: 153
    • View Profile
(No subject)
« Reply #4 on: July 04, 2005, 05:46:34 pm »
About the chkTp() function...
Notice that after int a=0; the code within if is only executed if the string is empty, so for a non-empty string the function always returns 3.
if(line[strlen(line)]) doesn\'t make sense (it always passes 0 to if). Delete it and just write return 2; after the for loop.

DELETED: Sorry for misleading you, and thanks Androgos for poining it out :rolleyes: I shouldn\'t post anything without checking it first...

You really should learn to debug your own programs - you won\'t be able to code anything otherwise.
« Last Edit: July 05, 2005, 11:49:11 am by Entamis »

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #5 on: July 05, 2005, 05:33:09 am »
Quote
Originally posted by ramlambmoo
Uh jesus another one of these debug my c++ threads.  Id rather just write my own program, but maybe that wont help you much,

Anyway a few quesitons:
Firstly how is the program going to tell the difference between an octal value and a decimal value? It certaintly cant work it out- e.g the Number 5647 could be Octal or it could be decimal.  It could even be Hex.  Im guessing you\'re going to need the user to tell them before hand what it is...


Octal values are entered with a zero in front (0888).
Hex value are entered with a 0x in front (0x123abc).
« Last Edit: July 05, 2005, 05:33:20 am by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

Androgos

  • Guest
(No subject)
« Reply #6 on: July 05, 2005, 09:05:41 am »
Quote
Originally posted by Entamis
(because in line[a++] a is increased before the expression is evaluated).


int i = 0;
printf(\"%d\\n%d\\n%d\\n\",i++,i,++i);
Output:
0
1
2

Conculsion: i++ is increased AFTER the use of it, and ++i is before

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #7 on: July 09, 2005, 04:24:01 am »
Also, out of curiousity, would it be considered more elegant if my code went something like this:


Code: [Select]


include
using namespace std;

void func1();
int func2();

int main()
{
//stuff
func1();
//more stuff
func2()
return 0;
}


func1()
{
//stuff that\'s in func1
}

func2()
{
stuff that\'s in func2
}



Would this be better seeing as I\'m making int main() more visible?
« Last Edit: July 09, 2005, 04:27:19 am by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.