Thank you nezachem! It's working perfectly now. Those loops and return commands always get me.

These are incredibly easy. I'm pretty new to C++ and I can tell you're just starting out. Answers to these are no doubt in your book. Or if you're too lazy to flip the pages a couple google searches will answer these for you. Just step through each problem logically. They're simple math equations. In your programs: 1) identify your variables 2) plan your algorithm 3) make some psuedocode to help it all make sense in your head 4) look through notes, your textbook, the internet to find ways to translate your psuedocode to c++ code 5) do the work

[QUOTE=Banfa;1766398]Your MatrixType must surely store a std::vector (or array) of oneitem and the length of that array (vectors provide their length). You need to iterate over the whole array testing each location until you find one that matches or return 0.0 if none match.[/QUOTE]

My matrix[] array stores an array of structs (oneitem). I have a private member variable length as well that stores the number of significant values from the matrix (i.e. numerous oneitem datas). I tried to implement a for loop for testing each value in the array of structs, but I can only seem to return the value of the first significant value. All others are beeing returned as 0 for some reason unknown to me. (I have my current code posted above)

[QUOTE=Banfa;1766398]With a std::map you could have a key class

Then declare your map [icode]std::map<MatrixKey, float>[/icode], use it internally to MatrixType when you need to find a value create a MatrixKey for row/col look it up in the map and if it isn't found return 0.0;[/QUOTE]

I can't use a dense matrix. I'm trying to store a dense matrix filled with numerous 0 values as an array of structs, which only holds significant values (ones not equal to 0). I am then trying to display that array of structs in the same format as the original dense matrix, so main is unaware that the matrix is beeing stored differently from a full 2D array.

I noticed that in another of my class member functions I increment length so I changed to using a temp value for my valueAt function.
[CODE]float MatrixType::valueAt(int i, int j) const
{ // code to return the float value at
// significant location row i col j
int temp = 0; // temp value holder
for(; temp < MAXSIZE; temp++){
if(i == matrix[temp].row && j == matrix[temp].col)
return matrix[temp-1].value;
} // checks to match up row and col to find correct value
return 0.0;
} // end for loop
} // end accessor valueAt[/CODE]
The function I had incrementing member variable length is my storeItem function:
[CODE]void MatrixType::storeItem(int i, int j, float v)
if(v != 0)
{ // if element is significant store members into array of structs
matrix[length].row = i; // row index stored
matrix[length].col = j; // col index stored
matrix[length].value = v; // value index stored
// now element length in the matrix has the value v and
// can be identified by members row and col
length++; // increment for next significant value
} // end if: the value has been stored at row and col
// else
// value is insignificant (ie is zero)
// move on to next element
} // end mutator storeItem[/CODE]
I figured that by incrementing length in this function length's value was always the latest member in the array, so using it for comparison in my valueAt function would never yield the right value.

By changing ...

Well let's look at what you want to do logically.
[] prompt user to provide day of week
[] read user input
[] check user input for validity

  • if user input is valid; do what you need to do
  • if user input is invalid; prompt user about incorrect input and ask again

Since you don't know how many times the user may enter incorrect input you could use a do while loop to check for validity. That is logically, while(input is false/invalid) prompt user for new input. You will need a flag of type bool.

[CODE]bool inputValid; // should be true if input is accepted
cout << "That isn't a day of the week!\n" << "Please provide a day of the week - ex. Mo Tu We Th Fr Sa Su" << endl;
cin >> whichday;
// you'll need code in the if statement to check whichday against accepted input
if (/whichday is accepted then/) inputValid = true;
else /whichday is not accepted/ inputValid = false;
while(!inputValid); // while input is not valid[/CODE]

Hello all. I am having difficulty getting my program to output data correctly. I have a class member function that is supposed to return the value of a specific member of a struct element based on identification with its (the struct element's) other members.

[code] float MatrixType::valueAt(int i, int j) const
{ // code to return the float value at
// significant location row i col j
if(i == matrix[length].row && j == matrix[length].col)
return matrix[length].value;
// checks to match up row and col to find correct value
return 0.0;
} // end accessor valueAt[/CODE]

The matrix[] array is of type struct.

The struct is as follows:
[CODE]struct oneitem
int row, col;
float value;

I know that I need to somehow change the count of the array (i.e. length) so that it searches through the entire array of structs to find one with matching row and col. I'm not sure if my if statement for comparison is correct either.

In a broad sense what I am trying to do with this program is input a two-dimensional matrix from .txt file and save only the non-zero values of the matrix into a one-dimensional array of structs. Then I want to take that one-dimensional array of structs and output its values so as to recreate the original two-dimensional matrix. I am effectively trying to take a dense matrix filled with numerous 0s and save it more effeciently (using an array of structs; I'm not up to par with pointers). ...

Sorry about the poor clarity in what I've been asking. The input file is a .txt file specified by the user. The file contains a list of 4 hex digit (4 refering to how many digits per instruction) instructions and follow standard machine language opcodes:
0000 No OP
1RXY Load register R with the value of memory location XY; rR = mXY
2RUV Load register R with the value UV; rR = UV
3RXY Store register R to memory location XY; mXY = rR
40SR Copy register S to register R; rR = rS
5RST Add registers S and T and place result in register R; rR = rS + rT
6RST Subtract registers S and T and place result in register R; rR = rS – rT
7RST OR registers S and T and place result in register R; rR = rS OR rT
8RST AND registers S and T and place result in register R; rR = rS AND rT
9RST XOR registers S and T and place result in register R; rR = rS XOR rT
A1RU rotate the bits in register R, U places to the right; rR = rR rotated U places to right
BRUV set the program counter to UV, if the value of
register R is the same as the value of register 0; PC = UV if rR == r0
C000 Halt
D000 No OP
E000 No OP
F000 No OP
I'm not worried about C000-F000.
To interpret each instruction, read the ...

Your eGrades and sNum haven't been declared anywhere in the program.

Perhaps if I shared my code and expounded on what I am trying to accomplish it would be easier to help.



using namespace std;

typedef enum { LOAD='l', RUN='r', CLEAR='c', DUMP='d', QUIT='q', MEMORY='m', REGISTERS='g' } optionType;

int run ();
int load();
void dump(char gpr[], char progCount, int instReg);
void clearMemory();
void clearRegisters(char gpr[], char& progCount, int& instReg);
void clearAll();

optionType menu();

int main(int argc, char *argv[])
bool done = false; //Menu control
char gpr[16]; //General Purpose Registers 0 through F
char progCount = '0'; //Program Counter
int instReg = 0; //Instruction Register

while (! done)
          case LOAD:  if (load()!= 0) done = true;    break;
          case RUN:   if (run() != 0) done = true;    break;
          case DUMP:  dump(gpr[16]/*I am having problems with this section getting the argument to work correctly*/, progCount, instReg);break;
          case CLEAR: clearAll();                     break;
          case MEMORY: clearMemory();                 break;
          case REGISTERS: clearRegisters(gpr[]/*same here. it says it's expecting an expression*/, progCount, instReg);           break;
          case QUIT:  done = true;



int run()
cout << "Run program from address specified by user\n"; //specified by PC
return 0;

int load()
char filename[50]; //stores filename
ifstream fin; //used for file input

//Prompt user for file name
cout << "Enter file name: ";
cin.getline(filename, 50);;

{ //if file can't open exit program

char word[50];
fin >> word;
{ //echo back the values read in 2 digit hex form for each number, ...

If I were to use the ios_base does that affect the entire program or just a specific block of code? And if I were to change the basefield, would I be able to continue using arrays like I would if I were in decimal form?

So is hexadecimal already a pre-programmed data type? Like I can just use the insertion and extraction operations to modify the base of the number?

I've been scouring the internet and my textbooks and haven't found any good explanations about how to incorporate hexadecimal numbers into my C++ code. I am currently working on a program that acts as a simulated computer environment to run a simulated machine language in the console a maximum of 16 instructions long. I already have the code required to read the instruction set from a .txt file and am working on loading it into an array representing memory. I know that machine language instructions are hexadecimal instructions 4 bytes long. My problem is trying to find a way to get my program to read hexadecimal digits as it would any integer or char.

I was debating on whether or not to make a class where I could assign integer values to A, B, C, D, E, and F. But I'm not sure where to go from there. Any help to move me in the right direction is greatly appreciated.

I'm not extremely familiar with classes, but I do suppose I could look more in depth into them. When creating a class, would you suggest is contained all the cards in a deck (so any user input can be called from it and compared in another function) or would it just hold what is in the user hand?

I'm reading about two-dimensional and multi-dimensional arrays at the moment in order to better organize user input. I'm trying to create a function that can store a card player's hand. At the moment I'm just brainstorming how it would look and writing out some notes to myself to organize it in plain english before I break it down into C++.

So far I've figured this:
I'm going to use a two-dimensional array to store cards in a dealt hand. I figured it would have two columns, one to store the card's value and one for its suit. So something like this:
[CODE]string cardArray[row][col]; //Where row is however many cards are in the hand[/CODE]
I'm not quite sure if string is the right data type to go about this.

In program execution I want the user to enter suit (S for spade, H for heart, D for diamond, and C for club) followed by a space then the face value in int form (1-13 with the Ace as 1, Jack as 11, Queen as 12, and King as 13).

Since the suit is a char/string and the face value is an int I wonder if it would be best to create two seperate one-dimensional arrays for suit and value then use a cin statement to store the values. My only concern is being able to call on the values and compare them against each other to determine little flags (only card in suit or no cards in a specific suit for ...

Alright. I have the code working now, it took a little bit of playing around with and a bit of reading on my part. I just thought I would post the working code for anyone who may visit this thread in the future.

[CODE]int part2(){
// Ask user to provide the number of hours, minutes, and seconds.
cout << "\nWelcome to Second Calculator." << "\nEnter the number of hours, minutes, "
<< "and seconds (in HH:MM:SS format): "
<< endl;

string str; cin >> str; // Used to find and break apart the hh, mm, and ss from the input string and store their
                        // repective values.

string::size_type hourEndPosition = str.find_first_of(":");
hourToCalc = atoi( str.substr(0,hourEndPosition).c_str() );

string::size_type minuteEndPosition = str.find_first_of(hourEndPosition+1,minuteEndPosition);
minToCalc = atoi( str.substr(hourEndPosition+1, minuteEndPosition - hourEndPosition-1).c_str() );

string::size_type secondEndPosition = str.find_first_of(minuteEndPosition+1,secondEndPosition); 
secLeft = atoi( str.substr(minuteEndPosition+1).c_str() );

int secTotal = (hourToCalc * 3600) + (minToCalc * 60) + secLeft; // Formula for calculating total number of seconds.

// Display the calculated nummber of seconds from user given hours, minutes, and seconds.
cout << "From the given " << hourToCalc << ":" << minToCalc << ":" << secLeft 
     << " the total number of seconds is equal to: " << secTotal << endl;

return 0;


@Red Goose - I wish it were that simple! That places spaces in there that aren't supposed to be there. Thank you though, I never knew that I could do that in the console!

@Fbody - I was just writing about that and have been scearching through my C++ textbooks. Thanks for the confirmation!

@firstPerson - That actually makes a lot of sense to me. I never knew about the atoi (I believe it's ASCII to Integer, at least from what I've read now). For seconds I would just really have to do this:

string::size_type secondEndPosition = str.find_first_of(minuteEndPosition+1,secondEndPosition);

int seconds = atoi( str.substr(minuteEndPosition, secondEndPosition - minuteEndPosition);

I'm going to try this out now and see what happens!

I am currently writing a program that can calculate the number of total seconds in a user given number of hours, minutes, and seconds (in that order in hh:mm:ss format hh, mm, and ss being any number from 0-99). I'm not really worried for checking for errors in user input, I'm more concerned with the user inputing code into the console in that format (hh:mm:ss).

Enter time in format hh:mm:ss: 2:15:30
Number of seconds is 8130

This is my program so far:

using namespace std;

int secToCalc; int minToCalc; int hourToCalc; int secLeft; int minLeft; int hourLeft;

// Take the input from the user and calculate the number of seconds through
// multiplication. 3600 sec = 1 hour AND 60 sec = 1 min

int main(){
// Ask user to provide the number of hours, minutes, and seconds.
cout << "Welcome to Second Calculator." << endl << "Enter the number of hours, minutes, and seconds (in that order): "
<< endl;
cin >> hourToCalc >> minToCalc >> secLeft; // Get hours, minutes, and seconds from user.

int secTotal = (hourToCalc * 3600) + (minToCalc * 60) + secLeft; // Formula for calculating total number of seconds.

// Display the calculated nummber of seconds from user given hours, minutes, and seconds.
cout << "From the given " << hourToCalc << ":" << minToCalc << ":" << secLeft 
     << " the total number of seconds is equal to: " << secTotal << endl;

system("pause"); // Prevent console from closing ...