Clinton Portis 211 Practically a Posting Shark

looks good to me. the only things that could go wrong is an incorrect arraySize being passed in, or the examGrades[ ] array wasn't initialized properly (in this case, all elements to zero), which would cause random stuff from memory to reside in the array.

Clinton Portis 211 Practically a Posting Shark

1. How is the data arranged in your file? Please provide an example if that's not too much to ask.

2. A vector object can handle a file of infinite size and should be the container of choice for this project. Tell your instructor that a vector is an array.

3. Push your file into your vector array.

4. Sort your array as desired.

5. Create/open a new file to write to.

6. Write your vector to the new file.


You state that you are having difficulty writing to a file. It's as simple as creating on ofstream object, calling the open( ) function, and supplying a path/name/extention. Using the same ofstream object, use the << insertion operator to send data to the file. It's just like a cout << operation, just using a different stream.

Clinton Portis 211 Practically a Posting Shark

This is really a self explanitory type of assignment. Straight-forward, no exotic techniques required.

I will assume since requirement #3 mandates the use of PEMDOS precidence, requirements #1 & #2 do not; therefore, we can get away with working "left-to-right" for requirements #1 & #2.

Requirement #1: We have to assert that the user entered equation matches number of operations. If not, throw errror and back out to main menu. Push the equation into a single string object. Identify the number of operators, you could use the count() function from <algorithm>. Number of operators should equal number of operations. Parse the string into a terms[ ] string array, you can use the <string> class substr() function for string parsing. Pass each term into a user defined function that will return a 'double' type result of each term. You can use the <sstream> class to perform string-to-int conversion. Accumulate the result into a double type 'subtotal' variable. Repeat until all terms have been processed.

Requirement #2: Actually a little easier than req. #1 since we do not have to perform operators == operations assertion. Push user entered expression into a single string. Parse each expression into a token[ ] string array. Pass each token into a user defined function for processing. Use the <sstream> class to perform string-to-int conversion. Accumulate the results from each token into a double type 'subtotal' variable.

Requirement #3: Perform the same steps at req. #2 up …

kvprajapati commented: Neat & clear. Very good explanation. +11
Clinton Portis 211 Practically a Posting Shark

try this:

for(int i=0, size=sV.size(); i<size; i++)
{
     for(int j=0; j<size; j++)
     {
          if(sV[i].compare(sV[j]) == 0)
          {
               sV.erase(sV[j]);
          }
     }
}
Clinton Portis 211 Practically a Posting Shark

couple of ways you could do this, I will show you using a vector object, which will allow you to read in a file of any size. A vector has many of the same behaviors as an array so it's easy to use. Here is a list of all the member functions you get with vector. The push_back() function is most common:

#include<vector>
#include<string>

vector<string> file;
string line;

//read in a line of data
while(getline(infile, line))
{
     //push the data into the back of the vector array
     //unlike an array of fixed size, you can read in a file of any size
     file.push_back(line);
}

//Now you can access or display your data just like an array
for(int i=0, size=file.size(); i<size; i++)
{
     cout << file[i] << endl;
}

In summary, vector class objects are easy to use because they exhibit the same behavior as arrays and also provide member functions that allow you to store data of unknown size.

Clinton Portis 211 Practically a Posting Shark
str1 = "$#!%";
str1 += ' ';
str1 += " the ";
str1 += ' ';
str1 += " police.";
Clinton Portis 211 Practically a Posting Shark

Here is a muy bueno tutorial on all the cool features of the dos console, to include changing colors:

http://www.adrianxw.dk/index.html

Clinton Portis 211 Practically a Posting Shark

line #29 should be

return Number(fmod(x, rhs.x));

Youtube: basic c++ skills playlist http://www.youtube.com/watch?v=o4Pf8C793Ko&feature=&p=68244A805BD16617&index=0&playnext=1

Clinton Portis 211 Practically a Posting Shark

you could probably handle your modulus operation like this:

#include<cmath>

class Number
{
     public:

     Number(){x = 0.0;}
     Number(double a){x = a;}
     Number operator= (Number rhs);
     Number operator% (Number rhs);

     private:

     double x;
};

friend ostream &operator<<(ostream &stream, Number n){stream << n.x; return stream;}


Number Number::operator=(Number rhs)
{
     x = rhs.x;
     
     return *this;
}

Number Number::operator%(Number rhs)
{     
     return Number(fmod(x, rhs));  

}

Now you should be able to do stuff like this:

int main()
{

     //Using overloaded constructor, overloaded assignment, and default constructor:
     Number num1(235.9), num2 = 6.9, temp;
    
     cout << "My Number: " << num1 << endl;

     temp = num1 % 2.57;

     cout << "Implied cast modulo: " << temp << endl;

     temp = num1 % num2;

     cout << num1 << " % " << num2 << " is " << temp;

     return 0;
}

This is uncompiled and untested, it is for conceptual purposes only; point ye' in the right direction.

Here is a good youtube on operator overloading: http://www.youtube.com/watch?v=hiog9Y-683w

Clinton Portis 211 Practically a Posting Shark

Just out of curiosity, how are you learning to code in c++
(ex: what method are you using, book, online tutorial, school professor...)

Clinton Portis 211 Practically a Posting Shark

in your function, try the following at #17:

string temp1, temp2;

while(getline(fin, temp1))
{
     //Handle even number of lines
     if(getline(fin, temp2))
     {
          fout << temp2 << endl;
          fout << temp1 << endl;
     }
     //Handle odd number of lines
     else
     {
          fout << temp1;
     {
}
Clinton Portis 211 Practically a Posting Shark

1. if argc != 3, throw error message. exit program.
2. create an ifstream object that will handle all file reading operations.
3. create on ofstream object that will handle all file writing operations.
4. open the file using your ifstream object, using argv[0] as the file to open.
5. perform some sort of error checking to see if the file was actually opened.
6. create a new file using your ofstream object, using argv[1] as the file to create.
7. loop through the file, reading character at a time; write the char to the output file.
8. if (char_count == atoi(argv[2])) write a '\n' newline to file
9. close the ifstream object
10. close the ofstream object
11. return 0;

Clinton Portis 211 Practically a Posting Shark

This is a double post. You just posted the same question about an hour ago.

You have identified that you have 2 different possibilities for file input:

1) Date, char, 4 digits, int.

2) Date, char, 4 digits, string, double, double.

I would recommend reading in your file line at a time. This will allow you to parse and test the line for it's contents and identify which of the 2 formats you have and respond accordingly.

Probably the easiest cheat method to identify one format over the other, is just to count the number of white spaces; the first format will have 3 white spaces, the other will have 5:

#include<sstream>
#include<string>
#include<vector>


vector<string> tokens;
stringstream line;
string word;

while(getline(infile, line))
{
     while(word << line)
     {
          tokens.push_back(word);
     }

     if(tokens.size() == 4)
     {
          //Handle a line containing 4 pieces of data
     }
     else
     {
          //Handle a line containing 6 pieces of data
     }   

     tokens.clear();  

}

Now you have the ability to be the coolest kid in your computer science class and possibly pick up some hot nerd chicks because you can handle varying formats of data from your file.

Clinton Portis 211 Practically a Posting Shark

There are generally 2 different strategies for reading a file; read the file line-by-line (which preserves the white spaces) using getline() and then parse the line into the stuff you need, or read the file word at a time using the >> extraction operator, which is white space delimited.

Clinton Portis 211 Practically a Posting Shark

Your request is very vague. I actually still have no clue what you are asking for. If I had to guess, you require a bar graph that will display in percent a user entry vs. proper zip-code format. For example, if a user enters 48$43-0020, 8 out of 9 characters qualify as a correct zip code format, which is 89%. A resulting bar graph could look like this:

0 10 20 30 40 50 60 70 80 90 100
**********************************


If this even close to what you are asking for, no one knows except for you. Please, enlighten us; unlock the mystery that is your c++ problem.

Clinton Portis 211 Practically a Posting Shark

I think in lines #81 and #82 you have to use the -> dereferrence operator as opposed to the dot operator.

Clinton Portis 211 Practically a Posting Shark
//Begin loop: 0 to 40 (years)

     //Year_Total = Year_total + Monthly_payment * 12

     //If the Loop_Counter / 2 has no remainder, then it has to be a "2nd Year"
          //Monthly_payment = Monthly_payment * 1.10

//End Loop
Clinton Portis 211 Practically a Posting Shark

Ifstream's extraction operator is overloaded to handle every native c++ datatype, to include char's.

In your implementation, you should be using >> data extraction for everything.

Clinton Portis 211 Practically a Posting Shark

instead of using getline(), you should continue your use of the >> extraction operator. unlike getline(), the extraction operator depends on 'white space' delimiters.

the way you are doing it now, you are trying to do is to store the entire first line into bevName :s

Clinton Portis 211 Practically a Posting Shark

another afterthought,

you could populate your int arrays normally, and then perform some type of 'reverse' operation on them whenever you are ready to use them.

Clinton Portis 211 Practically a Posting Shark

According to this documentation:

http://www.cplusplus.com/reference/algorithm/search/

The search() function is overloaded to provide 2 variations of the function to the user, depending on program needs; the first version takes 4 args, the second takes 5.

Clinton Portis 211 Practically a Posting Shark

1. you should #include<algorithm>

2. you create 'string line;' but then never assign anything to it..

3. so when you call search(line.begin(), line.end(), search.begin(), search.end(), nocase_compare), arguments #1 and #2 will return iterators to a string of nothing.


for more information: http://www.cplusplus.com/reference/algorithm/search/

Clinton Portis 211 Practically a Posting Shark

You are using int[] arrays...

Since you have requested hints and not code, i'll write out a suggested process in pseudocode:

1. using a loop, initialize all array elements to zero. Do this for every int[] array you are using.

2. populate your arrays from 'right-to-left' as opposed to the traditional left-to-right method. (this will probably involve changing loop conditions to start at then end of the array and decrement backwards towards the [0] element)

3. any elements that do not get populated will hold a zero value :)

Clinton Portis 211 Practically a Posting Shark

You will never.. ever.. ever.. see an assignment operation take place in boolean if/else logic.

The only things you will ever use for comparison are the following: <, >, >=, <=, ==, !, !=, && and ||. These are all you are allowed to use.. ever. No exceptions.

Anything else (like a = assignment operator) will always be wrong. Therefore, line #17 in your code is wrong.

So anytime you use if( ), you know you must abide by a very restrictive set of rules.

Clinton Portis 211 Practically a Posting Shark

what type of arrays are you using? are they int[], char[] string[]???

Clinton Portis 211 Practically a Posting Shark

good program..

i'll throw in my 2 cents for improvement... take it or leave it:

this will only initialize the [0][0] element:

int ranges[first_dim][second_dim]={0};

instead ye' should try this:
(otherwise you'll have mystery stuff in your array.. and you won't know why it's there)

for(int i=0; i<4; i++)
{
     for(int j=0, j<5; j++)
     {
          ranges[i][j] = 0;
     }
}

Delete lines #126 through 130; move line #119 to line #112.
Delete lines #212 through 216; move line #204 to line #196.

Otherwise, good job on ye' program.

Clinton Portis 211 Practically a Posting Shark

I have never seen an assignment operator in boolean if/else logic... ever.

I have only seen boolean operators, such as: <, >, <=, >=, ==, !, && and ||.

And yes, that semicolon should not be there.

Clinton Portis 211 Practically a Posting Shark

I believe your problems are here:

//line #78
if (currentPlayer ='X')

//and line #81
else (currentPlayer = 'O');

You are making a common mistake when it comes to boolean comparison.. take a minute, think about it, and tell us what ye' come up with.

In fact this error is so common, I believe compilers should throw warnings whenever this is detected.

Clinton Portis 211 Practically a Posting Shark

If ye' are trying to display an ascii escape character as text, simply add an extra \ backslash before each escape character.

Clinton Portis 211 Practically a Posting Shark

You will have to write the spaces between the numbers:

catOut << stuff << ' ' << more_stuff << ' ' << endl;
Clinton Portis 211 Practically a Posting Shark

I suspect a '\n' newline character is being left in the input buffer when the user presses the ENTER key. Try using cin.ignore() right after line #24 to force the leftover newline character to be disregarded by your program.

Clinton Portis 211 Practically a Posting Shark

1. When reading in your pgm file, I would recommend reading it into 2D array:

int pixels[256][256];

2. You could read in the pgm file like this:

//Get file type
catIn >> type;
//Get file dimensions
catIn >> length >> width;
//Get grayscale max value 
catIn >> grayscale_max;
//Get pixels
for(int i=0; i<256; i++)
{
     for(int j=0; j<256; j++)
     {
          catIn >> pixels[i][j];
     }
}

3. Now that you have a nicely packaged pgm file.. all you need is that almighty emboss function:

void emboss(int& pixels[][])
{
     int result = 0;

     //I am using loop conditions that will start the emboss operation at the lower-right non-border pixel 
     //and work backwards through the pgm file

     //backwards row traversal
     for(int i=254; i>0; i--)
     {     
          //backwards column traversal
          for(int j=254; j>0; j--)
          {
               //Perform 'emboss math'
               result = (pixel[i+1][j+1] - pixel[i][j]) + 8;
               
               if(result < 0)
               {
                    result = 0;
               }
               else if(result > 15)
               {
                    result = 15;
               }

               //Assign the new 'emboss' value 
               pixel[i][j] = result;   
          }
     }
}

4. All there is left to do now is to use your catOut object to open a new file and write the pgm file header info and pixels[][] to file. It will conveniently look similar (yet opposite) to step #2.

All of this code is untested. It is just an idea of how I'd probably go about doing this. There are probably many many ways to get this done. If anyone see's a better way to do this …

Clinton Portis 211 Practically a Posting Shark

Very good then.. I was considering that where you start the emboss operation makes a difference as it affects pixels progressivley through the pgm file (starting at the upper-left corner would have a different effect than starting at the lower-right corner for example)

Having a fixed 256 256 file size is kinda cool, although it would be simple to account for pgm files of any size.

Give me a chance to grab a beer or 2 and I'll knock out some pseudo code for ye'.

Clinton Portis 211 Practically a Posting Shark

I am still a little unclear on how to perform an 'emboss' operation.. I'd hate to give you wrong code based on my misunderstanding.

From what I understand, we first identify a (non-border) pixel to emboss. In this case, we'll attempt modify the "15" pixel:

P2
4 4
15
0  0   0  0
0 [b]15[/b]  10  0
0  7   5  0
0  0   0  0

Then we identify the pixel located to the 'lower-right', which in this case is the "5" pixel (which can be a border pixel):

P2
4 4
15
0  0   0  0
0 [b]15[/b]  10  0
0  7   [b]5[/b]  0
0  0   0  0

In this example, according to your emboss forumla, we have (5 - 15) + 8 = -2 which is < 0. Therefore, the 15 pixel when embossed becomes 0.

This is my understanding of the emboss operation thus far.. am I close?
(Doesn't make sense to me.. turning a white pixel to black)

-dw

Clinton Portis 211 Practically a Posting Shark

1. you never initilize the 'count' variable.. it could be anything. (you should also do the same for 'average' as good practice)

2. you should decrement the count variable by 1 before using it in calculations in order to account for an extra loop iteration in the case of a -1 entry.

3. you should move line #14 outside of the loop.

Clinton Portis 211 Practically a Posting Shark

Hello Ms. Christina,

Let me first say that I have never done any work with .pgm files. I've just looked at some .pgm examples though, and it doesn't look too terribly difficult. However, I think we might have to put our brains together to come up with the result that ye' be looking for.

With that said, I am unclear when it comes to "subtracting the lower right and subtracts the center pixel." If you can provide me a notepad example of a .pgm file, and show me what it means to perform the subtraction, I am sure I can help you on the file I/O, array and pixel manipulation.

-Dave W.

ChristinaS commented: Fab! Thx! +2
Clinton Portis 211 Practically a Posting Shark

one thing that i notice is that you got a couple of smiley faces in your code.

Clinton Portis 211 Practically a Posting Shark

I could write the code for you...

or I could explain a simple concept relating char and int types and let you experiment and come up with working code on your own.

What you are asking is quite simple, instead of using 'char' types directly (which are representation from the ascii table) you could store the ascii value of character in an 'int' data type. Example:

char x;
int  y;

y = 82; 
x = y;
 
cout << y << " is the ascii value for " << x;

Given a little bit of knowledge (and not having the complete code done for you) suprise us with some code of your own.

Clinton Portis 211 Practically a Posting Shark

Just take my code above and edit to create what I asked.

would you like a side of fries with your order..

Clinton Portis 211 Practically a Posting Shark

why are you even taking this class..

Clinton Portis 211 Practically a Posting Shark

I just threw some code your way to get you started.. maybe jog some of them brain cells. I purposely did not finish your assignment.

This was my thought process (although somewhat impaired after just comming back from a fresh concert last night featuring Axe Murder Boyz opening up for ABK):

1. create a counter that will hold student's test performance based on 5 test ranges: 0-29, 30-49, 50-69, 70-89, and 90-100. Each second-dimension element will serve as a 'counter' of how many test results qualified for each range:

//First  Dimension: [0] is the assignment, [1] is lab, [2] is test, and [3] is exam
//Second Dimension: [0] is 0-29, [1] is 30-49, [2] is 50-69, [3] is 70-89 and [4] is 90 to 100

int ranges[4][5];  

//to make our array easier to read, i'll also do the following quick little trick:
//assign = 0, lab = 1, test = 2, and exam = 3

enum{assign, lab, test, exam};

2. Now that I got a counter, I needed a function that I could plug in test information and will return which element of ranges[][] to populate:

int get_range(int& grade)
{
     if(grade < 30)
     {
          //increment ranges[][0]
          return 0;    
     }
     else if(grade >= 30 && grade <= 49)
     {
          //increment ranges[][1]
          return 1;
     }
     else if(grade >= 50 && grade <= 69)
     {
          //increment ranges[][2]
          return 2;
     }
     else if(grade >= 70 && grade <= 89)
     {
          //increment ranges[][3]
          return 3;
     }
     else
     {
           //Anything else must …
Clinton Portis 211 Practically a Posting Shark

Administrator locked out my edit:

I will be decisive and assume that you need the number of A's, B's, C's, D's, and F's for the assignment, lab, test, and exam. A's will be 100% to 90%, B's, will be 89% to 80%, C's will be 79% to 70%, D's will be 69% to 60%. F's will be below 60%:

int get_grade(int& grade, char& test)
{    
     //test: A = assignment, L = lab, T = test, E = exam

     grade /= 10;

     switch(grade)
     {
          case 10:
          case  9:  return 0;     break;
          case  8:  return 1;     break;
          case  7:  return 2;     break;
          case  6:  return 3;     break;
          default:  return 4;     break;
     }
}

int grades[5];  // A's = [0], B's = [1], C's = [2], D's = [3], F's = [4]

 inFile    >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;

//Populate grade statistics
grades[get_grade(stumarks[i].progassn, A)]++;
grades[get_grade(stumarks[i].lab,  L)]++;
grades[get_grade(stumarks[i].test, T)]++;
grades[get_grade(stumarks[i].exam, E)]++;
Clinton Portis 211 Practically a Posting Shark

I will be decisive and assume that you need the number of A's, B's, C's, D's, and F's for the assignment, lab, test, and exam. A's will be 100% to 90%, B's, will be 89% to 80%, C's will be 79% to 70%, D's will be 69% to 60%. F's will be below 60%:

int get_grade(studmarks& s, char& test)
{
     //test: A = assignment, L = lab, T = test, E = exam

     int grade = 0;

     switch(test)
     {
          case 'A':  grade = s.progassn / 10;  break;
          case 'L':  grade = s.lab  / 10;      break;
          case 'T':  grade = s.test / 10;      break;
          case 'E':  grade = s.exam / 10;      break;
     }

     switch(grade)
     {
          case 10:
          case  9:  return 0;     break;
          case  8:  return 1;     break;
          case  7:  return 2;     break;
          case  6:  return 3;     break;
          default:  return 4;     break;
     }
}

int grades[5];  // A's = [0], B's = [1], C's = [2], D's = [3], F's = [4]

 inFile    >>stumarks[i].studentid >>stumarks[i].seperator>>stumarks[i].progassn
           >>stumarks[i].seperator>>stumarks[i].lab
           >>stumarks[i].seperator
           >>stumarks[i].test>>stumarks[i].seperator
           >>stumarks[i].exam;

//Populate grade statistics
grades[get_grade(stumarks[i].progassn, A)]++;
grades[get_grade(stumarks[i].lab,  L)]++;
grades[get_grade(stumarks[i].test, T)]++;
grades[get_grade(stumarks[i].exam, E)]++;
Clinton Portis 211 Practically a Posting Shark

When you say you need the "range of marks", are you asking for the the highest and lowest score for each exam?

Clinton Portis 211 Practically a Posting Shark

you never #include<iostream> or #include<string> or using namespace std;

you asked if line #3 is correct, it is correct; however, will continue to throw errors until you include the string library.

Clinton Portis 211 Practically a Posting Shark

just my two cents..

I'm not sure if you can derive your main() function from Deck.. (i've never done it this way) this would be new to me:

int Deck::main()

I would segregate your program into the standard three sections: .h header file, .cpp main driver, and .cpp function definitions (making sure the .h header has the same file name as it's .cpp function definitions)

Then, in your int main() driver .cpp, be sure to #include<Deck.h>

(Deck.h will automaticallly be associated with Deck.cpp because they have the same file name, so no need to #include anything)

Clinton Portis 211 Practically a Posting Shark

you might have to #include<string>

Clinton Portis 211 Practically a Posting Shark

I believe this version of your program will at least compile:

#include<iostream>
using namespace std;

int A[5];
int ctr = 0;

int main()
{
     ctr = 5;

     while(ctr < 5);
     {
          cin >> A[ctr];
          ctr++;
     }

     cout << A[5];

     ctr = 5;

     do
     {
          cout << A[ctr];
          ctr++;

     }while(ctr <= 4);

     for(ctr = 0; ctr <= 4 ; ctr++)
     {
          cout<<A[ctr];
     }

return 0;
}
Clinton Portis 211 Practically a Posting Shark

your search algorithm will probably look something like this:

bool found = false;
int index = 0;

cout << "Enter random zip code: ";
cin >> zip_code;

for(int i=0; i < max_number_of_cities; i++)
{
     if(zip_code == mystruct[i].zip)
     { 
          found = true;
          index = i;
          break;
     }
}

if(!found)
{
     cout << "zip code not found in database.";
}
else
{
     cout << "the zip code " << zip_code << " is for " << mystruct[index].city_name;
}
Clinton Portis 211 Practically a Posting Shark
string stuff[10];

stuff[2] = "infinity";

for(int i=0; i<10; i++)
{
     cout << stuff[i] << endl;
}