Archived

This topic is now archived and is closed to further replies.

SSJCORY

Suggestions on my code?

Recommended Posts

A friend suggested that a good way to get better at C++ would be to make a calculator that reads straight from a string. Say if the user typed: 4+5-3*3/3 it would work. But, he suggested that i print out all of the tokens of the array first in order. I think I did a good job, but if anyone has any comments, suggestions that would make my code better, I am all ears... Eyes anyways. Here''s my code.
#include<iostream.h>
#include<stdlib.h>
char input[100];
int numdigits = 0;
int numofnumbers = 0;
char signs[100];
int numofsigns = 0;
char number[100][100];
int realnumber[100][100];
bool isDigit(int inquestion){
	if(input[inquestion] == ''0'' ||input[inquestion] == ''9'' ||input[inquestion] == ''8'' ||
		input[inquestion] == ''7'' ||input[inquestion] == ''6'' ||input[inquestion] == ''5'' ||
		input[inquestion] == ''4'' ||input[inquestion] == ''3'' ||input[inquestion] == ''2'' ||
		input[inquestion] == ''1''){
		return true;
	}
	else{
		return false;
	}
}
char getInputType(int inquestion){
	return input[inquestion];
}
void getInput(){
	cout<<"Input your problem:";
	cin>>input;
}
void convertArray(){
	for(int arrayslot = 0; arrayslot < 100; arrayslot++){
		if(isDigit(arrayslot) == true){
			number[numofnumbers][numdigits] = input[arrayslot];
			realnumber[numofnumbers][0] = atoi(number[numofnumbers]);
			numdigits++;
		}
		if(isDigit(arrayslot) != true && numdigits > 0){
			signs[numofsigns] = getInputType(arrayslot);
			numofsigns++;
			numofnumbers++;
			numdigits = 0;
		}
	}
}
void printTokens(){
	int i;
	for(i = 0; i < numofnumbers; i++){
		cout<<realnumber[i][0]<<", "<<signs[i];
		if(signs[i] != NULL){
			cout<<", ";
		}
	}
	cout<<"Last token is complete."<<endl;
}
int doMath(){
	int answer = 0;
	for(int domath = 0; domath <= numofsigns; domath++){
	}
	return answer;
}
void main(){
	getInput();
	convertArray();
	printTokens();
}
Thanks guys. Don''t know where I''d be if I didn''t have the people here at gamedev.
Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you |Smeagol: My little hobbitses.|

Share this post


Link to post
Share on other sites
You should check out the STL library and some of the older libs that standard C had, you''ll find great help there, but I''ll do you a favor and show you some to start with .. BTW, this code is standard C++, but either that standard was set after VC++ 6 or microsoft just plain sucks it, because you have to get some updates to get this code to work (it''ll work on all good compilers perfectly)

check this code out..


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

int main(void)
{
string str = "";
cout << "
enter a string: " << endl;
cin >> str;
for(int x=0; x<str.length(); x++)
{
if(isdigit(str[x])) cout << "
digit " << x << " is a digit" << endl;
}

return false;
}


your includes don''t have the .h extension on purpose, it is not used in newer compilers. also note that the old C library ctype has the prefix "c", because.. well.. it is an old C library. first lets note the isdigit function included in the ctype library. It should cut a good few lines out of your program and make it easier on the eyes

Now check out string. string is a class defined by the including of "string" at the top. you use it much like a C string, but you don''t have to set a specific width to use it. C strings are are fine, but what if the user types in more characters than you store? most likely something crashes, and perhaps an especially l33t hacker could do some damage. i''m not vouching for the security of string, i didn''t write it, but someone who is much better than me most likely did. Basically it makes your code more robust and much easier to write.

this is da FUTURE

PS - a book called "STL Programming from the Ground Up" is a great reference for all the new features now standard in C++.

Share this post


Link to post
Share on other sites
All those extra libraries make coding too easy... They make it like java. If I were to code this in java it would be no challenge. When I am forced to use array's it is much more beneficiary to me i think because I have to think about what I am doing. In java and with the string.h file you get these easy little methods like .length(), .substring()".substr() in C++", and a few others. Bottom line I like to do things the hard way
Thanks a lot for the input.
P.S. Why do they use the namespace std thing. Isn't it easier just to type .h? Or is there some other use for namespace std?


Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don't need you |Smeagol: My little hobbitses.|



[edited by - ssjcory on October 20, 2003 8:40:19 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by SSJCORY
All those extra libraries make coding too easy...
Bottom line I like to do things the hard way


Or, you could stop acting like its ''l33t'' to do things the hard/dumb way, and instead use the time you save to make something more complete/bug free/impressive.

Share this post


Link to post
Share on other sites

I general You should
-------------------------------------------------------
1. Try to reduce you dependancy on global varables.
2. Comment more.

II A cool way to solve this problem
-------------------------------------------------------
Try googling ''Recursive Descent Parser'' for an easy way to create a more complete calculator.

III A specific way to improve you code
--------------------------------------------------------
About your code first your function ''isdidgit'' could be replaced with a line that looks like

bool isDigit(char inquestion){
return (inquestion >= ''0'' && inquestion<=''9'')
}

This allows isDigit to be more generic you give it a char it retuns true if it is a digit.

My first game&& My Resume&& My Homepage

Share this post


Link to post
Share on other sites
I live in Jacksonville, I''m 16, Don''t think I am friends with a kid named Craig...



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you |Smeagol: My little hobbitses.|

Share this post


Link to post
Share on other sites
quote:
Original post by SSJCORY
All those extra libraries make coding too easy... They make it like java.

That's good. If you have a choice between a mature library and your own code that does the same thing, choose the library. After all, others have done it before and probably much better than you could, so why waste your time? Eventually you're going to learn that productivity is much more important than trying to show off your "mad coding skillz" by refusing to use libraries. Using what's available to you to solve problems effectively is a major part of being a good programmer.

[edited by - twix on October 20, 2003 9:40:47 PM]

Share this post


Link to post
Share on other sites
Fortunately, I hate that kids friend Craig. He claims he makes games and cant even tell me the hello world code. Cant wait to paintball him just to laugh at him when I bunker him.

Fort Walton Beach here, sorry bout the confusion and off topic.

Share this post


Link to post
Share on other sites
I know how to use libraries and I normally do. It''s just that for this particular thing I wanted to use only standard things. I wanted to see if I could do it. I can use strings and they are much easier to work with. I still have to familiarize myself with many libraries bc if I haven''t seen it in someone elses code I most likely don''t know that it exists. Like someone told me that there was already a library that had isdigit() in it but I had never seen it before. So sometimes it is bc I don''t want to use the libraries... other times I don''t know that they exist. Thanks.
P.S. I don''t wish to show off my coding skills... Because I know that they don''t compare with hardly anyones here on gamedev. I know I have a lot to learn. I don''t ever do something to make me look smarter than I really am.



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you |Smeagol: My little hobbitses.|

Share this post


Link to post
Share on other sites
quote:
Original post by SSJCORY
It's just that for this particular thing I wanted to use only standard things.

Not to belabor this point, but the Standard Template Library (along with the C Standard Library, from whence came isdigit() ) is about as standard a part of C++ as you can have.

[edited by - twix on October 20, 2003 9:51:27 PM]

Share this post


Link to post
Share on other sites
Like I said I don''t know about things I don''t use. Oh well I''ll learn as I go along.



Favorite Quotes:Gandalf: You shall not pass!|Smeagol: We don''t need you!|Sloth: Hey you guys!|

Share this post


Link to post
Share on other sites
In addition to the good suggestions above,
here's a couple of minor ones:

if(is_digit(arrayslot) == true
can be replaced with
if(is_digit(arrayslot))

and
if(is_digit(arrayslot) != true && numdigits > 0)
with
else if(numdigits>0)

You just checked to see if it was a digit in the if statement above. And even then,
isDigit(arrayslot) != true
could just be
!is_digit(arrayslot)

It's less to think about; you learn to think if 'expr' is true, so it takes more time to digest the extra compare to true or false. You shouldn't compare to true, 0 is false, all else is true (in C we used ints for booleans, and you still can/do/will in C++).

The standard C++ convention is_lots_of_underscores, though some people do use camelCase.

If you are going to use cin & cout, you may as well use std::string.

std::string str;
std::getline(cin, str);

...
quote:
Original post by twix
quote:
Original post by SSJCORY
All those extra libraries make coding too easy... They make it like java.

That's good. If you have a choice between a mature library and your own code that does the same thing, choose the library. After all, others have done it before and probably much better than you could, so why waste your time? Eventually you're going to learn that productivity is much more important than trying to show off your "mad coding skillz" by refusing to use libraries. Using what's available to you to solve problems effectively is a major part of being a good programmer.



Yes, but if you can't/don't know how to write a function like isdigit well, you're not going to be able to write a good lexical compator. Often you run into limitations in the standard library (xsp the C libraries), and you need to be able to roll-your-own. In C++, if you can write functors, you won't have to hand code so much.

This is practice, not production.

Is addi


[edited by - Magmai Kai Holmlor on October 20, 2003 10:18:56 PM]

Share this post


Link to post
Share on other sites