NathanOliver 429 Veteran Poster Featured Poster

the first is a class inside a class and the second is two separate classes

NathanOliver 429 Veteran Poster Featured Poster

Hey All,

I just finished my code and it seams to work fine. I'm looking for advice on improving it or making it easier to understand. Its not Fully complete because I only have to set up to take in a number less than 2147483648 because i am using an int. I know that can be changed, but before I get it fully polished I would appreciate your insights.

Thanks for Reading
Nathan

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int GetNumber(std::string number)
{
	std::string words[31] = {"one", "two", "three", "four", "five", "six", "seven",
 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen","nineteen", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety","hundred", "thousand", "million", "billion"};
	int numbers[31] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,30, 40, 50, 60, 70, 80, 90, 100, 1000, 1000000, 1000000000};
	for (int i = 0; i < 31; i++)
	{
		if (number == words[i])
			return numbers[i];
	}
	return 0;
}

int ConvertTextToNumber(std::string sentence)
{
	int number = 0;
	std::vector<std::string> parts;
	std::string tempString;
	size_t size = sentence.size();
	for (size_t i = 0; i < size; i++)
	{
		if (sentence[i] == ' ')
		{
			parts.push_back(tempString);
			tempString.clear();
		}
		else
		{
			tempString += sentence[i];
		}
	}
	parts.push_back(tempString);
	size = parts.size();
	std::vector<std::string> formula;
	formula.push_back(parts[0]);
	for (size_t i = 1; i < size; i++)
	{
		if (parts[i] == "and")
			continue;
		if (parts[i] == "hundred" || parts[i] == "thousand" || parts[i] == "million" …
NathanOliver 429 Veteran Poster Featured Poster

well if you have the read the data in from the file. store the names into an array of a string and the gpa into an array of doubles. I'm not exactly how you are supposed to be sorting the information. the trick is that when you are sorting one of the arrays you need to do the swap in the other array. lets say you have

string names[20];
double gpa[20];

when you swap gpa[2] and gpa[5] you also need to swap names[2] and names[5].

NathanOliver 429 Veteran Poster Featured Poster

First yes you would make an associative container. second the reason i made it char[][] is so that you can store all of the words in one array. if it was just char[] you could have only one word. like char temp[] = "five"; . having it as char[][] you can do char temp[3][20] = {"one", "two", "three"}; . the first part is for how many words you need and the second part is for the size for each word.

NathanOliver 429 Veteran Poster Featured Poster

Alright. I would suggest using two arrays then. the first array would be of type char[][] and the second array would be of type int[]. the first array you would populate with all of the cases that you are going to encounter. start it with "one" then "two" and keep going to "nine". then you will need to add "ten" through "ninety". after that you just have to "hundred", "thousand" and so on. with the integer array you would populate it the same way but you would use the actual number. with those two arrays you can then run through the string and match a word in the string to one of the words in the char[] and then you can use the index you found the word in to find the decimal value in the integer array.

NathanOliver 429 Veteran Poster Featured Poster

how are you getting the string in from the user? is it of type string or is it a c-style string i.e. char[]?

NathanOliver 429 Veteran Poster Featured Poster

You are right to assume that there will be some code to write but its not that much. If you are going from a five hundred and fifty to 550 needs 3 tables. If you can use the STL than using a map you would be a great way to go. If you cant use The STL then it gets a little more complicated but not much. Let Me know if you can use STL and I can help you Figure out how to code this.

NathanOliver 429 Veteran Poster Featured Poster

yes you can compare a int to a hex number. this should work

UINT32 foo = 20;
if (foo == 0x14)
    cout << "woot";
else
    cout << "dooh";
NathanOliver 429 Veteran Poster Featured Poster

Why don't you think your first piece of c++ code wont work?

NathanOliver 429 Veteran Poster Featured Poster

I would have to say that using global variables should not be used. IMHO all variables should either be in a function like main or in a class. It looks cleaner and is better understood if you pass a variable to a function instead of making it global and just using it in a function.

[EDIT]
I have never used a singleton class but it kinda seems like cheating to me.
[/EDIT]

NathanOliver 429 Veteran Poster Featured Poster

if you are trying to be really efficient with the amount of ram you are using and you need a variable that would hold anything from -10 to 10 then a singed char would work and you are only using 1 byte of memory.

NathanOliver 429 Veteran Poster Featured Poster

What happens if you move your srand call on line 26 to line 35?

NathanOliver 429 Veteran Poster Featured Poster

Setting you variables to zero on lines 85 and 103 isn't what you should do. You should initialize them to the first value in the array and then check from there. On line 89 you are checking if the lowest temp is less than the array value and if it is make the array value the lowest temp. You should be doing it the other way around. There are other errors but you should be able to start from here.

NathanOliver 429 Veteran Poster Featured Poster

What do you mean saying its no longer random? Are you getting the same results each time?

NathanOliver 429 Veteran Poster Featured Poster

if you are asking how to ask for a password then just ask for it inside the while loop. Replace password = "abc123"; with code to ask the user for there password and then retrieve the password.

NathanOliver 429 Veteran Poster Featured Poster

why are you putting a conditional inside the array index?

if (paths[sROW][sCOL]==' ' && paths[sROW+1<=19 /*this doesn't make sense*/][sCOL]
NathanOliver 429 Veteran Poster Featured Poster

I know this isn't a solution but it should help. If you know that when using your function trying to calculate a factorial higher than 31 will be incorrect, then you can use an if statement to check the number supplied to the function to see if it is higher than 31. If it is then simply return some number that would mean an error condition. Then in the function that calls your factorial function you can check the return and make sure there wasn't an error.

NathanOliver 429 Veteran Poster Featured Poster

Line 7 should be

void Initialize(Book[], int);

And line 26 should be

void Initialize(Book arrbook[],int size)
NathanOliver 429 Veteran Poster Featured Poster

see if this works

if (toupper(input) == *(name[0].c_str()))
NathanOliver 429 Veteran Poster Featured Poster

what is the error?

NathanOliver 429 Veteran Poster Featured Poster

Doing the way you have done would not require 60 cases but why would you want to do it this way? It seems to me that it adds another layer of complexity. Since you are still using the if statements why not do the calculation in the body of the if statement?

//...
if (checks < 20) {check_fees = 0.10 * checks;} 
else if (checks => 20 && checks < 40) {check_fees = 1.90 + 0.08 * (checks - 19)} 
else if (checks => 40 && checks < 60) {check_fees = 1.90 + 1.52 + 0.06 * (checks - 39);}
else {check_fees = 1.90 + 1.52 + 1.14 + 0.04 * (checks - 59)}
//...
NathanOliver 429 Veteran Poster Featured Poster

@ Nathaniel using a switch statement would be less than ideal in this case because you have a large range of numbers to work with. Even with fall through you would still need to have 60 cases.

NathanOliver 429 Veteran Poster Featured Poster

have you tried uninstalling and reinstalling the video driver?

NathanOliver 429 Veteran Poster Featured Poster

did you try pulling the cmos battery for a few minutes and then put it back in then give it a boot?

NathanOliver 429 Veteran Poster Featured Poster

But there is no need for him to pass it by reference. I'm not saying you cant but asking why you would want to.

NathanOliver 429 Veteran Poster Featured Poster

are you using a loop to read the file and check it against your permuted word?

NathanOliver 429 Veteran Poster Featured Poster

To expand on what Gerard pointed out is when you pass an array to a function it actually only passes a pointer to the first element. Since you are working with pointers any change to a value in the array in a function is reflected in main.

void foo(int[]);
// or
void foo(int[SOME_NUMBER]);

// is the same as
void foo(int *);
NathanOliver 429 Veteran Poster Featured Poster

you need to include is_prime.h in your is_prime.cpp file.

NathanOliver 429 Veteran Poster Featured Poster

No problem. Glad to help.

NathanOliver 429 Veteran Poster Featured Poster

Move line 21 above line 20 and make cout << s; part of the if statement.

bigstring = s;
if (bigstring.find(substring) != -1)
{
    cout << s << endl;
}
NathanOliver 429 Veteran Poster Featured Poster

can you post your latest code

NathanOliver 429 Veteran Poster Featured Poster

Well if you have to have bigstring ang substring then you can do

substring = city;
while (getline(file, s))
{
    bigstring = s;
    //...
}
NathanOliver 429 Veteran Poster Featured Poster

A1 in your f2 function is actually using A and not A1.

NathanOliver 429 Veteran Poster Featured Poster

Part of your problem is that your coding style is making it difficult to line parts of the code. The brackets don't always line up and that makes it difficult to find the mismatch. I would suggest you separate each function into its own header file so that you don't have one file that has 750 lines of code. It makes it much easier to find where problems are that way. here has some good examples on coding style

NathanOliver 429 Veteran Poster Featured Poster

You should be putting your if statement inside the while loop. The way the program should work is you should read in a city name and then check to see if that city matches or partially matches the city the user entered. If it does output the city name that you read in. If it does not the return to the top of the loop.

NathanOliver 429 Veteran Poster Featured Poster

first you need to have a return statement at the end of your mean function to return avg. Also your function prototype and your function definition don't match. Your prototype has a array and a int but you definition has no variables.

float mean()
{
    //...
    return avg;
}

Then in your cout statement you can do:

cout<< "The average of the array is:" << mean() <<endl;
NathanOliver 429 Veteran Poster Featured Poster

use the pow function.

foo = pow(bar, 3);
kra9853 commented: humorous and helpful +1
NathanOliver 429 Veteran Poster Featured Poster

to test if it has a decimal you can do something like

size_t spot;
string test = "123.456789";
spot = test.find_first_of(".",0)
if (spot == std::string::npos)
    cout << "there is a decimal";
NathanOliver 429 Veteran Poster Featured Poster

do you know of the string class?

NathanOliver 429 Veteran Poster Featured Poster

you cant because you are reading the file into an int. you will need to read it into a string and then test to see if has a decimal or not.

NathanOliver 429 Veteran Poster Featured Poster

on lines 15 and 17 you are setting your counter variable to -1. when it tries to access element -1 it throws an error. try setting the variables to 0 and see what happens.

NathanOliver 429 Veteran Poster Featured Poster

This is a nice conversion function at will convert any type that has an << operator.

#include <string>
#include <sstream>

template<typename T>
string ConvertToString(T & value)
{
    std::stringstream ss;
    ss << value;
    return ss.str();
}
NathanOliver 429 Veteran Poster Featured Poster

i would say your best bet is to walk through your code with the debugger and see what is happening.

NathanOliver 429 Veteran Poster Featured Poster

So whats wrong with it? Does it not work or are you getting a compiler error?

NathanOliver 429 Veteran Poster Featured Poster

on line 19 it should be if (found) . the same applies to line 22. also why is found a bool?

[EDIT]
what are you trying to do with this found = 2 * a > b; ?

NathanOliver 429 Veteran Poster Featured Poster

you need to seperate your while loops.

// you have
while (getline(infile, character)) 
 {
        myStack.push(character);
 

    while (!myStack.empty()) 
	{
       outfile << myStack.top() << endl;
       myStack.pop();           
    }


 }

// should be

while (getline(infile, character)) 
{
        myStack.push(character);
}
 

while (!myStack.empty()) 
{
       outfile << myStack.top() << endl;
       myStack.pop();           
}

also im not sure if you want to use getline. if you want to get each word in the file into its own element in the stack you will have to use ifstream's >> operator like i used in my example.

}

NathanOliver 429 Veteran Poster Featured Poster

since you dont know what the size of the file will be i would suggest using a container from the STL. that said i will use a vector for my example.

vector<string> words;
string word;
ifstream fin("text.txt")  // use ifstream object to get info from file
while (fin >> word) // will read until there is no more info in the file
    words.push_back(word);  // add the word to the vector
NathanOliver 429 Veteran Poster Featured Poster

lines 19 through 22 are not actually doing anything. you need to get the words from the file in order to push them into the stack. To get the word from the file into your stack you will need to do something like this.

stack<string> Stack;
string word;
ifstream fin("text.txt");
while (fin >> word)
     Stack.push(word);
NathanOliver 429 Veteran Poster Featured Poster

I would like to add that you should not be using void main. per the c++ standard main should always return a int.

NathanOliver 429 Veteran Poster Featured Poster

He is using global variables bradonrunyon.