• Advertisement

Archived

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

Suggestions on my code?

This topic is 5302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Where in florida do you live cory? How old are you also? I think I might know you. Kid named Cory, knows C++, lives nearby, has a friend named Craig.

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

  • Advertisement