Clinton Portis 211 Practically a Posting Shark

Here is a possible answer without using a Class.. you can use the algorithm(s) below when you put together your class in your program.

#include <iostream>
using namespace std ;

int main()
{
     int score[i], 
         total = 0 ;
     
     for(int i=0; i<10; i++)
     {
          cout << "\nEnter an integer: " ;  
          cin >> score[i] ;        
          total += score[i] ;
     }

     cout << "\n\n\tThe sum is: " << total ;

     cout << "\n\n\tThe average is: " << total / 10 ;


return 0 ; 
}
Clinton Portis 211 Practically a Posting Shark
else if (guess = num)

should be:

else if (guess == num)
Clinton Portis 211 Practically a Posting Shark
#include<iostream>
#include<ctime>
using namespace std;

int main()
{
    int number = 0,
        guess  = 0;        
    bool again = true;    
    char answer;
    
    srand(time(NULL));
    
    do{
                  
         number = rand()%3 + 1;
        
         cout << "\n\tGuess a number from 1 to 3: ";
         cin >> guess;
         
         while(again)
         {
            if(number == guess)
            {
               cout << "\n\tGood guess!  The correct number was " << number;
               again = false;
            }
            
            else
            {
                cout << "\n\tIncorrect!  "<< guess << " is not the right answer.";
                cout << "\n\tTry again.";
                cout << "\n\n\tGuess a number from 1 to 3: ";
                cin >> guess;           
            }
         }
           
         cout << "\n\n\tWould ye' like to try again? (Y/N) ";
         cin >> answer;
         
         again = true;
         
         }while(answer == 'Y' || answer == 'y');
         
   return 0;
}
Clinton Portis 211 Practically a Posting Shark
struct Record
{
   string title;
   int playtime;
   Record *next;
};

Record **ListOfRecords = new Record*[42];
Clinton Portis 211 Practically a Posting Shark

Here is one method you could use to sort your linked list alphabetically:
(I haven't tested this specific code but the idea is sound)

First, push each node into an array of nodes:

Node **array = new Node*[size];
Node *temp = head_ptr;

while(temp)
{
   Node[i] = temp;
   temp = temp->next;
}

Now sort your array of Nodes based on the first letter of each name:

#include<algorithm>

//This function sorts to ascending order
sort(Node[0]->name[0], Node[size]->name[0]);

Now.. all you have to do is include a provision on what to do if the first 2 letters of a person's name are the same.


Once you are done with all your sorting operations, turn the array of nodes back into a linked list. Make each node of the newly sorted list point to the next node in line by resetting all the *next pointers:

head_ptr = Nodes[0];

for(int i=0; i<size-1; i++)

   Nodes[i]->next = Nodes[i+1];

That's it. You now have a head_ptr that points to beginning of a sorted linked list.

Clinton Portis 211 Practically a Posting Shark

I had a little trouble with linked lists when I was learning.. so I whipped up a little something to get you started:

#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdlib>
#include<iomanip>
#include<cctype>
#include<string>
#include<windows.h>
using namespace std;


struct Node
{
   string name;
   double cost;
   int    quantity;   
   Node   *next;
};

class Soda
{    
   public:
          
      Soda();
      ~Soda();
      void reset();
      void display();            
      void get_soda_config();
      void set_soda_config();
      void load_machine();
      void dispense();
      void prompt();
      void calc_change();
      void gotoxy(int x, int y);
      void clrscr();
      void pop_nodes();
      bool again();
          
   private:           
      
      Node *head_ptr;
      HANDLE hOut;
      ifstream infile;      
      int selection,
          soda_quantities[5],          
          x_coords[5][15],
          y_coords[5][15]; 
      double amount,
             change,
             soda_prices[5];              
};

enum Flavors{ Cola, CreamSoda, Grape, LemonLime, RootBeer };       


int main()
{
    Soda Machine;    
 
    Machine.get_soda_config();
    Machine.set_soda_config();
    Machine.display();
    
    do{
                                                     
         Machine.prompt();
         Machine.dispense();          
         Machine.display();
         Machine.reset();
         
      }while(Machine.again()); 
     
   return EXIT_SUCCESS;
}



////////////////////////////////////////////////////////////////
////////////  Function Definitions   //////////////////////////
//////////////////////////////////////////////////////////////


Soda::Soda()
{            
   reset();   
   hOut = GetStdHandle ( STD_OUTPUT_HANDLE );
   
     for(int i=0; i<5; i++)
   {    
       soda_quantities[i] = 0;
       soda_prices[i] = 0.0;
   }
   
   for(int i=0, k=17; i<5; i++, k+=8)
      for(int j=0; j<15; j++)
      
           x_coords[i][j] = k;
           
   for(int i=0; i<5; i++)
      for(int j=0, k=31; j<15; j++, k--)
      
           y_coords[i][j] = k;
}

Soda::~Soda()
{             
   while(head_ptr)
   
      pop_nodes();
}      

void Soda::display()
{     
     WORD wOldColorAttrs;     
     CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
     
     clrscr();
     
     GetConsoleScreenBufferInfo(hOut, &csbiInfo);
     wOldColorAttrs = csbiInfo.wAttributes;     
     SetConsoleTextAttribute(hOut, BACKGROUND_RED | BACKGROUND_INTENSITY);
     SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_BLUE);
     
     cout << endl << endl << endl;
     cout << "\t __________________________________________     \n";
     cout << "\t|\\                                         \\  \n";
     cout << "\t| \\                                         \\ \n";
     cout << "\t|  \\ ________________________________________\\\n";
     cout << "\t|\\  |                                         |\n";
     cout << "\t| \\ |            Machine o' Drink             |\n";
     cout << "\t|  \\|_________________________________________|\n";
     cout <<
Clinton Portis 211 Practically a Posting Shark

does she like younger men

Clinton Portis 211 Practically a Posting Shark

the only time i can think of using friend functions is for operator overloading.. the rest of the time they should be avoided because they violate OOP data encapsulation.

Clinton Portis 211 Practically a Posting Shark
class Fractions
{

public:

     //class constructor
     Fractions( );

     //overloaded operators
     Fraction operator+(const Fraction &rhs);
     Fraction operator-(const Fraction &rhs);
     Fraction operator/(const Fraction &rhs);
     Fraction operator*(const Fraction &rhs);

     //other 
     void print( );

private:

     int numerator;
     int denominator;
}

This is about all the help I can give ye' for now.. Above is an example class that should get you started.. all you have to do is write the functions. :cool:

Clinton Portis 211 Practically a Posting Shark

wtf are you talking about...?!?!?@#?!

Clinton Portis 211 Practically a Posting Shark

i like agressive

RrrRRraawwrrrrr.....

Clinton Portis 211 Practically a Posting Shark

post what you have so far.. and we can make suggestions.

Clinton Portis 211 Practically a Posting Shark

Have to follow forum policy:

We only give homework help to those who show effort
Jun 8th 2004 until Jul 9th 2008


This forum is meant for discussing programming languages in addition to exchanging code and algorithms. However, it has become a problem where too many students are posting homework problems expecting a quick solution without ever trying for themselves. Not only does this constitute cheating, but it is very discouraging, frustrating, and annoying to everyone who takes valuable time to answer programming support questions.

Though we are all here to help, please don't expect quick solutions to your homework. We'll help you get started, exchange algorithm ideas, how-to's, etc. but only if you show that you're willing to put in effort as well.

For more information about this, click this link or this link.

Thanks for understanding and for your cooperation! These rules are here to make DaniWeb a student-friendly place to learn.

Clinton Portis 211 Practically a Posting Shark
#include<iostream>
using namespace std;

int main()
{
     //declare a couple of int variables
     //initialize your variables

     //cout a prompt for the user to enter the lower boundry
     //cin the user's input into one of your previously declared int variables

     //cout a prompt for a user to enter the upper boundry
     //cin the user's response into one of your previously declared int variables

     //use a loop with a counter starting value equal to the lower boundry
          //accumulate += the lower boundry until it equals the upper boundry

     //cout your final answer

    return 0;
}
Clinton Portis 211 Practically a Posting Shark

this program is wicked easy.. at least try to make an attempt on your own.

Clinton Portis 211 Practically a Posting Shark
int blah (Treep *tree);

I think the data type you are attempting to pass is incorrect.. you should be passing in an object of type, "node"

int blah (node *tree);

please post compiler errors for more assistance.

Clinton Portis 211 Practically a Posting Shark

This is what I am thinking...

Your loop condition is based on a=1 and will increment while a < size()+1.

for(int a=1;a<(S.size()+1);a++)

But my guess is.. your linked list is "zero based".. much like how an array is.. which would account for why all other elements would be deleted.. except for your very first one.. which is, "node zero" or the head node..

try this and see what happens:

for(int [b]a=0[/b]; a<S.size()+1; a++)
Clinton Portis 211 Practically a Posting Shark

Here is a little something to get ye' started:

#include<iostream>
using namespace std;




////////// User Defined Abstract Data Types ////////////

struct Record
{
     int quiz_1 ;
     int quiz_2 ;
     int midterm ;
     int final ;
} ;


class Grade_Calculator
{

     public:

     void menu( ) ;     
     void add_a_record( ) ;
     void delete_a_record( ) ;    
     void calculate_grade( ) ;
     void display_results( ) ;

     private:

     int number_of_records ;
     Record students[30] ;     //Initialize an array of 30 "Records"    
                               //Assuming 30 students or less
} ;




//Place any other non-related user defined function prototypes here








/////////  Driver Program /////////

int main( ) 
{
     Grade_Calculator myGC ;    //Create an object of type, "Grade_Calculator"  (gain access to the "Grade_Calculator" class)

//Do

     //Display a menu
     //Get menu option
     //Respond to menu option

//While (the user wishes to continue) ;

return 0 ; 
}






/////////////////////////////////////////////////////////////
/////////////////  Function Definitions /////////////////////
/////////////////////////////////////////////////////////////

/*function return type (if any)*/ Grade_Calculator::function_name( )
{
     //function algorithm
}

/*function return type (if any)*/ Grade_Calculator::function_name( )
{
     //function algorithm
}

/*function return type (if any)*/ Grade_Calculator::function_name( )
{
     //function algorithm
}

/*function return type (if any)*/ Grade_Calculator::function_name( )
{
     //function algorithm
}
Clinton Portis 211 Practically a Posting Shark

C++ was invented by Bjarne Stroustrup, a professor at Texas A&M. It evolved from the C language.. and was originally referred to as, "C with Classes." The International Standards Organization now governs the c++ language.

Here is a good interview with Bjarne that will answer a lot of ye' questions. For a more comprehensive list of interviews, click here. Also, be sure to check out The Design and Evolution of C++

Clinton Portis 211 Practically a Posting Shark

free webhosting is good.. howev3r, I think that when it comes to the highest quality assignments done fast.. I think a nominal $$$ will get you what you want in this case.. ;)

Clinton Portis 211 Practically a Posting Shark

when it comes to my dev-cpp debugger.. when I try to go into debug mode.. I am prompted that, "Your project does not have degubbing information, do you want to enable degubbing and rebuild your project?" I select yes, the code is then recompiled.. and then execution stops. why? I don't know.. Here is the compilation log:

Compiler: Default compiler
Building Makefile: "F:\Dev-Cpp\Makefile.win"
Executing make clean
rm -f cablesplicer.o cablesplicer.exe

g++.exe -c cablesplicer.cpp -o cablesplicer.o -I"F:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"F:/Dev-Cpp/include/c++/3.4.2/backward" -I"F:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"F:/Dev-Cpp/include/c++/3.4.2" -I"F:/Dev-Cpp/include"

g++.exe cablesplicer.o -o "cablesplicer.exe" -L"F:/Dev-Cpp/lib" -mwindows

Execution terminated
Compilation successful

Why this happens with only windows projects..??!? I have no idea..

As far as MVSC++ goes.. I have issues with making any windows projects.. I open up a new windows project.. I write the code.. always get this one single error:

--------------------Configuration: cablesplicer - Win32 Debug--------------------
Compiling...
cablesplicer1.cpp
f:\cablesplicer\cablesplicer1.cpp(437) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

cablesplicer.exe - 1 error(s), 0 warning(s)

for some reason, mvsc++ doesn't even appear to recognize my <windows.h> or StdAfx.. or something.. I've tried to play around with the settings and stuff.. even re-installation does not help..

I had breakpoints set in all the above scenarios.


So.. I am down to using my dev-cpp and msvc++ debuggers only for DOS console projects..


I think maybe because.. I have two hard drives.. and I installed my compilers on my F: …

Clinton Portis 211 Practically a Posting Shark

I just want to say thank you very much for your help.. for some reason, my dev-cpp and mvsc++ debuggers don't work for windows projects.. I originally put all that code under WM_PAINT just mainly for testing purposes.. and then later on split it up into one or two seperate functions.. but obviously there were fundamental problems with where my code was located and how it was being called..

very cool catch on the return value from EM_GETLINECOUNT.. who would have thought that it would always return at least one line..?!?! This means that my parsing functions was going to be called on windows creation.. when there is no text.. and attempt to parse a lot of nothing into memory.

anyway.. moved some code around.. life == good.. been at a standstill for about a week.. made a breakthrough today :cool:

Clinton Portis 211 Practically a Posting Shark

Here is the algorithm as a DOS console project...

Clinton Portis 211 Practically a Posting Shark

Having troubles handling text from a multiline edit box.. the first part of this algorithm runs fine by itself.. the second part (highlighted in blue) runs fine as a DOS console project using 'char' data types.. but when I add it to this windows project (using TCHAR's) it distorts the GUI display and causes the program to lock-up/terminate... why would it work so well as a dos console project.. but totally go bezerk as a windows project...??!!


The entire code thus far can be viewed here (for like up to 3 days)


Problem code:

void EditBoxFileParser(HDC hdc, PAINTSTRUCT* ps, HFONT hFont, HWND hwnd, HWND hEdit)
{
     
     int iCount; 
     WORD iLength, iOffset;        
     TCHAR **Lines;
     
     //Get Number of Lines in Edit Field
     iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
     
     //If Editbox is empty, exit function
     if(!iCount)
     
          return;
     
     Lines = new TCHAR*[iCount];
     
     //Populate 2D array, Lines[LineIndex][LineText]
     for(int i=0; i<iCount; i++)
     {    iOffset = SendMessage(hEdit, EM_LINEINDEX, i, 0);
          iLength = (WORD)SendMessage(hEdit, EM_LINELENGTH, iOffset, 0);
          Lines[i] = new TCHAR[iLength+sizeof(WORD)+1];
          CopyMemory(Lines[i], &iLength, sizeof(WORD));          
          SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
          Lines[i][iLength] = '\0';
     }   
     
     //Visually Verify Lines[][]
     for(int i=0, x=300, y=275; i<iCount; i++)
          
             TextOut(hdc, x, y+=20, Lines[i], lstrlen(Lines[i]));   

     
[b]/* Code works fine up until this point */[/b]

//The code below works fine by itself in as a DOS application
//But locks up this windows project
 
    //Parse User Entered Text
    TCHAR **Name = new TCHAR*[iCount];
    TCHAR **FiberCount1 = new TCHAR*[iCount];
    TCHAR **FiberCount2 = new TCHAR*[iCount];;
    int word_index;
    bool cont = true;
    
    for(int
Clinton Portis 211 Practically a Posting Shark

*names[2] will only have two elements..


*names[0] and *names[1] ;)

Clinton Portis 211 Practically a Posting Shark

Is this your entire code..?? if so, it has some serious issues.. like missing header files.. and missing main( ) function...

if not, you should always check to see if your file was opened correctly.. could use if( instream_object.fail( ) ) cout << "file open failure!";

also.. I have a fealing that maybe some of the stuff you are reading in from the file might be exceeding the bounds of one or more of the cstring arrays.. (common segfault problem)

post all ye' code along with a sample file you are attempting to read from

Clinton Portis 211 Practically a Posting Shark

Got anything a little easier? :)

How about this:

#include <windows.h>

#define ID_LIST 1
#define ID_TEXT 2

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "ComboBox App";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "ComboBox App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,
Clinton Portis 211 Practically a Posting Shark

why not make your own isspace() function then.. ;)

Clinton Portis 211 Practically a Posting Shark

Haven't seen any of your code.. but here is my guess:

int i=0, word_count=0;

//If no text head been read, exit the function
if(!myText.size())

     return;

//Test to see if there are any non-white spaces
bool flag=FALSE;
while(!Flag && myText.substring(i, 1))
{
     if(!isspace(myText.substring))
 
          flag=TRUE;
     i++;
}

if(flag==FALSE)
{
     cout << "No Text Entered!";
     return;
}

//Perform word count - first word
i=0;
while(myText.substring(i, 1))
{
     
     if(isspace(myText.substring(i, 1)) && !isspace(myText.substring(i+1, 1)))

          word_count++;

     i++;
}

//"First Word" provision
word_count++;

like i said.. haven't seen any of your code.. just throwing something your way to think about :cool:

Clinton Portis 211 Practically a Posting Shark

sounds like you are on the right track.. counting the number of spaces is probably the easiest way to calculate the number of words in a file.. but as you have already identified.. one could run into trouble if there are multiple spaces in between words..

so why not identify the number of situations where there is a space and the next character is not a space...

#include<cctype>

if( isspace(string[i]) && !isspace(string[i+1])

     ++word_count;

the above code is not perfect.. you'll have to make considerations for the first word for example.. but should get ye' on the right track ;)

Clinton Portis 211 Practically a Posting Shark

Trying my hand at extracting user entered data from a multiline edit box... my strategy is to first, get the number of total lines from the edit box.. and the length of each line from the edit box.. and create a dynamic 2D array that is NULL terminated at the second dimension.. then read in the edit box line at a time..

Came up with a little TextOut( ) loop just as a method to visually verify if my 2d array was loaded correctly.. and made a WM_COMMAND case to respond to the, "Add Count" pushbutton and call the EditBoxFileParser( ) function... which should load and display the contents of the Lines[][] 2d array.

Please take a look when ye' get a chance.. at runtime, the user should be able to enter stuff in the edit box.. then click the, "Add Count" pushbutton.. and a display of the edit box conents should appear somewhere off to the right side of the screen..

At this point, nothing happens when I enter stuff in the edit box and click the "add count" button..


Here is my editbox file parser function:

void EditBoxFileParser(HWND hwnd, HWND hEdit)
{
     
     int iCount, iLength;
     TCHAR **Lines;
     
     //Get Number of Lines in Edit Field
     iCount = SendMessage(hEdit, EM_GETLINECOUNT, 0, 0);
     
     Lines = new TCHAR*[iCount];
     
     //Populate 2D array - Lines[LineIndex][LineText]
     for(int i=0; i<iCount; i++)
     {     
          iLength = SendMessage(hEdit, EM_LINELENGTH, i, 0);
          Lines[i] = new TCHAR[iLength+1];
          SendMessage(hEdit, EM_GETLINE, i, (LPARAM)Lines[i]);
          Lines[i][iLength+1] = '\0';