I have to create a gradebook that reads in any amount of students and what their grades are. The user can add more to the matrix if they need another column of grades. I am having a problem with my addGrades function and need help on what is worng and how I can fix it. Here is the function for my "Add Grades" function:
void AddGrades(vector%26lt;String%26gt; %26amp;name, TTTBoard %26amp;Board)
{
int Row = Board.numrows() - 1;
int Col = Board.numcols() - 1;
//Board.resize(Col,Row);
//for(int j=0;j%26lt;= Col;j++){
for(int i = 0; i %26lt; Row; i++){
cout%26lt;%26lt;"Please enter a grade for "%26lt;%26lt;name[i]%26lt;%26lt;" ";
cin%26gt;%26gt;Board[Row][i]; //j,i ?
Row++;
}//end for
//}//end for
}//end fun.
//Thanks.
C++ maxtrix?
void AddGrades(vector%26lt;String%26gt; %26amp;name, TTTBoard %26amp;Board)
{
int Row = Board.numrows() - 1;
for(int i = 0; i %26lt; Row; i++){
cout%26lt;%26lt;"Please enter a grade for "%26lt;%26lt;Name[i]%26lt;%26lt;" ";
cin%26gt;%26gt;Board[i];
}//end for
}//end fun.
Reply:This if for a console app fight? Hence the cout statements? If so, the cin statement only needs to be seperated by commas.... and why are you using { instead of ; for the endings of a few of your lines of code?
floral design
Monday, May 24, 2010
Saving the enter key in array..c++??
cin.get cant do that nor can cin%26gt;%26gt;
so how do i do it??
Saving the enter key in array..c++??
cin%26gt;%26gt;"here witte the arry as you predifine"
this can save your arry.
so how do i do it??
Saving the enter key in array..c++??
cin%26gt;%26gt;"here witte the arry as you predifine"
this can save your arry.
C++ heap problem (new fuction)?
How do I create an array of Employees at run time? The size of the array is entered by the user.
I tried...
cin %26gt;%26gt; num;
Employee[] emp = new Employee[num];
but its wrong.
C++ heap problem (new fuction)?
Does it need to be an array? Why don't you use a vector?
this is what vectors are used for.
Reply:This should work fine:
Employee* pemp = new Employee[num];
Then pemp[0] to get the first element, pemp[1], etc.
Ensure you never read past pemp[num-1] as that is not your memory.
Reply:1. use a array of character pointers (string)
2. then create a buffer say buffer[255] that will accept user input
3. get the length of the string entered
4. then use malloc to allocate storage for the string
5. copy the entered string to the storage allocated in 4
6. do steps 4 to 5 for every new employee
Reply:You need to initialize your Employee (class) to zero (0) before you create the array. I believe this is called the "default constructor". Also, when you the function has run its course, you must destroy the array with: delete Employee; . Otherwise, the memory (heap) manager will cause a run time error. Remember, C++ does not have automatic garbage collection, though Java does.
I tried...
cin %26gt;%26gt; num;
Employee[] emp = new Employee[num];
but its wrong.
C++ heap problem (new fuction)?
Does it need to be an array? Why don't you use a vector?
this is what vectors are used for.
Reply:This should work fine:
Employee* pemp = new Employee[num];
Then pemp[0] to get the first element, pemp[1], etc.
Ensure you never read past pemp[num-1] as that is not your memory.
Reply:1. use a array of character pointers (string)
2. then create a buffer say buffer[255] that will accept user input
3. get the length of the string entered
4. then use malloc to allocate storage for the string
5. copy the entered string to the storage allocated in 4
6. do steps 4 to 5 for every new employee
Reply:You need to initialize your Employee (class) to zero (0) before you create the array. I believe this is called the "default constructor". Also, when you the function has run its course, you must destroy the array with: delete Employee; . Otherwise, the memory (heap) manager will cause a run time error. Remember, C++ does not have automatic garbage collection, though Java does.
C++ Strings?
I have to take in a a string and scramble it 5 times, using string methods. I have to separate it using 2 radnom num's, and into three parts. It won't execute for some reason. Any help will be appreciated. Here's the code:
cout%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter a phrase to shuffle: ";
getline(cin,phrase_input);
for(int i = 0;i %26lt;= 5;i++)
{
beg = phrase_new.substr(0,random_one);
mid = phrase_new.substr(random_one,random_two - random_one);
endphr = phrase_new.substr(random_two,phr_len - random_two);
shuffled_phrased+= endphr;
shuffled_phrased+= beg;
shuffled_phrased+= mid;
}
cout%26lt;%26lt;shuffled_phrased;
C++ Strings?
first, I assume that somewhere you are copying phrase_input into phrase_new somewhere and just didnt show the code.
second, make sure your random_one and random_two are not greater than the length of the string phrase_new.
third, to loop 5 times, your "for" loop should be "for(int i = 0; i %26lt; 5; i++)"
fourth, you need to copy shuffled_phrased back into phrase_new at the end of your loop, so you can repeat the operations on it again.
Reply:Do you know how to use a debugger?
If not, I suggest you learn.
A debugger will help you to pinpoint a problem to a specific line of code. Learn to step through the code and verify it is doing what it is supposed to be doing.
Learn to use breakpoints and the call stack.
cout%26lt;%26lt;endl;
cout%26lt;%26lt;"Enter a phrase to shuffle: ";
getline(cin,phrase_input);
for(int i = 0;i %26lt;= 5;i++)
{
beg = phrase_new.substr(0,random_one);
mid = phrase_new.substr(random_one,random_two - random_one);
endphr = phrase_new.substr(random_two,phr_len - random_two);
shuffled_phrased+= endphr;
shuffled_phrased+= beg;
shuffled_phrased+= mid;
}
cout%26lt;%26lt;shuffled_phrased;
C++ Strings?
first, I assume that somewhere you are copying phrase_input into phrase_new somewhere and just didnt show the code.
second, make sure your random_one and random_two are not greater than the length of the string phrase_new.
third, to loop 5 times, your "for" loop should be "for(int i = 0; i %26lt; 5; i++)"
fourth, you need to copy shuffled_phrased back into phrase_new at the end of your loop, so you can repeat the operations on it again.
Reply:Do you know how to use a debugger?
If not, I suggest you learn.
A debugger will help you to pinpoint a problem to a specific line of code. Learn to step through the code and verify it is doing what it is supposed to be doing.
Learn to use breakpoints and the call stack.
C++ Vectors?
The instructions for this problem are:
" Write a complete program that reads in 100 double values from standard input (keyboard) into a vector. Then output all 100 values to standard output (terminal) in the same order they were read in with one space between each value."
I do not need to worry about prompting the user with instructions.
The code I have so far is:
#include %26lt;iostream%26gt;
#include %26lt;vector%26gt;
int main()
{
vector%26lt;double%26gt; values(99);
while( int i = 0 ; i %26lt;= 99 ; i++ )
{
cin %26lt;%26lt; vector[i];
}
for ( int i = 1 ; i %26lt; values.size()-1 ; i++ )
{
cout %26lt;%26lt; vector[i] %26lt;%26lt; " " ;
}
return 0;
}
Could someone please tell me what I am doing wrong? Thanks in advance.
C++ Vectors?
Here is correct code based on your code:
#include %26lt;iostream%26gt;
#include %26lt;vector%26gt;
using namespace std;
const static int vec_sz = 100;
vector%26lt;double%26gt; values(vec_sz);
int main( int ac, char * av[] )
{
for( int i = 0 ; i %26lt; vec_sz ; i++ )
{
cin %26gt;%26gt; values[i];
}
for ( int i = 0 ; i %26lt; vec_sz ; i++ )
{
cout %26lt;%26lt; values[i] %26lt;%26lt; " " ;
}
return 0;
}
Reply:cin %26gt;%26gt; values[i]; (not %26lt;%26lt;)
and cout %26lt;%26lt; values[i], not vector[i]
if the cin doesn't work (vector::operator[] returns a reference so it should in theory) use a temporary double variable first then assign that into the vector.
chelsea flower show
" Write a complete program that reads in 100 double values from standard input (keyboard) into a vector. Then output all 100 values to standard output (terminal) in the same order they were read in with one space between each value."
I do not need to worry about prompting the user with instructions.
The code I have so far is:
#include %26lt;iostream%26gt;
#include %26lt;vector%26gt;
int main()
{
vector%26lt;double%26gt; values(99);
while( int i = 0 ; i %26lt;= 99 ; i++ )
{
cin %26lt;%26lt; vector[i];
}
for ( int i = 1 ; i %26lt; values.size()-1 ; i++ )
{
cout %26lt;%26lt; vector[i] %26lt;%26lt; " " ;
}
return 0;
}
Could someone please tell me what I am doing wrong? Thanks in advance.
C++ Vectors?
Here is correct code based on your code:
#include %26lt;iostream%26gt;
#include %26lt;vector%26gt;
using namespace std;
const static int vec_sz = 100;
vector%26lt;double%26gt; values(vec_sz);
int main( int ac, char * av[] )
{
for( int i = 0 ; i %26lt; vec_sz ; i++ )
{
cin %26gt;%26gt; values[i];
}
for ( int i = 0 ; i %26lt; vec_sz ; i++ )
{
cout %26lt;%26lt; values[i] %26lt;%26lt; " " ;
}
return 0;
}
Reply:cin %26gt;%26gt; values[i]; (not %26lt;%26lt;)
and cout %26lt;%26lt; values[i], not vector[i]
if the cin doesn't work (vector::operator[] returns a reference so it should in theory) use a temporary double variable first then assign that into the vector.
chelsea flower show
Can someone please tell me what's wrong with my C++ program?this program should show how many students earned
a specific score. it is not working because i type inn 72 and it shows 20. the array only has two 72 scores. then, if i want to type inn another score, i have to type it twice in order for the screen to show an output. this is my code:
#include %26lt;iostream%26gt;
using std::cout;
using std::cin;
using std::endl;
int main()
{
//declare array
int count = 0;
int searchFor = 0;
int scores[20] = {90, 54, 23, 75, 67, 89, 99, 100, 34, 99,
97, 76, 73, 72, 56, 73, 72, 20, 86, 99};
//get input from user
while (searchFor != -1)
{
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
//search for score equal to the searchFor value
for (int x = 0; x %26lt; 20; x = x + 1)
if (scores[x] = searchFor)
count = count + 1;
//end if
//end for
//display cout
cout %26lt;%26lt; "Count: " %26lt;%26lt; count %26lt;%26lt;endl;
//get input from user
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
}//endwhile
return 0;
} //end of the searchArray function
Can someone please tell me what's wrong with my C++ program?this program should show how many students earned
There has to be == on the if statement
while (searchFor != -1)
{
// User enters a score
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
// If score is in the array, then add to count
for (int x = 0; x %26lt; 20; x++)
{
if (scores[x] == searchFor)
count++;
}
// Display count
cout %26lt;%26lt; "Count: " %26lt;%26lt; count %26lt;%26lt;endl;
}
return 0;
}//end of the searchArray function
#include %26lt;iostream%26gt;
using std::cout;
using std::cin;
using std::endl;
int main()
{
//declare array
int count = 0;
int searchFor = 0;
int scores[20] = {90, 54, 23, 75, 67, 89, 99, 100, 34, 99,
97, 76, 73, 72, 56, 73, 72, 20, 86, 99};
//get input from user
while (searchFor != -1)
{
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
//search for score equal to the searchFor value
for (int x = 0; x %26lt; 20; x = x + 1)
if (scores[x] = searchFor)
count = count + 1;
//end if
//end for
//display cout
cout %26lt;%26lt; "Count: " %26lt;%26lt; count %26lt;%26lt;endl;
//get input from user
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
}//endwhile
return 0;
} //end of the searchArray function
Can someone please tell me what's wrong with my C++ program?this program should show how many students earned
There has to be == on the if statement
while (searchFor != -1)
{
// User enters a score
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
// If score is in the array, then add to count
for (int x = 0; x %26lt; 20; x++)
{
if (scores[x] == searchFor)
count++;
}
// Display count
cout %26lt;%26lt; "Count: " %26lt;%26lt; count %26lt;%26lt;endl;
}
return 0;
}//end of the searchArray function
How do I get rid of the newline character in C++ using ignore?
I have this class structure, I'm trying to read in strings.
for (int count = 0; count %26lt; num; count++) {
cfile %26gt;%26gt; library[count].name.last;
cfile %26gt;%26gt; library[count].name.first;
getline(cfile, library[count].book.title);
cfile %26gt;%26gt; library[count].book.pages;
getline(cfile, library[count].book.genre);
getline(cfile, library[count].book.other.publisher);
cfile %26gt;%26gt; library[count].book.other.year;
}
And I asked this guy and he said:
After you cin%26gt;%26gt; name.first and name.last, you will have read "Jk" and "Rowling", respectively, but you've left the newline in the stream. The call to getline will receive an empty line, and when you try to convert that empty string to a number, you get something completely random like 211400.
How do I get rid of the newline character, something to do with cin.ignore????
How do I get rid of the newline character in C++ using ignore?
The guy you asked answered correctly. When reading from an instream object using %26gt;%26gt;, you have to be careful about newlines being left behind.
I could type up the solution here, but someone else has already done so. I'll give you the link: http://www.daniweb.com/tutorials/tutoria...
I'll summarize the two solutions you have. The first, and recommended for beginners to C++, is to avoid cin and use getline instead. The other way is cin.ignore(numeric_limits%26lt;streamsize%26gt;::m... '\n').
Please read the entire article. It is technically accurate as well.
In the future, use Google to find out answers. There's good resources at "C++ FAQ", cplusplus.com , cppreference.com.
Reply:cin.ignore('\n');
for (int count = 0; count %26lt; num; count++) {
cfile %26gt;%26gt; library[count].name.last;
cfile %26gt;%26gt; library[count].name.first;
getline(cfile, library[count].book.title);
cfile %26gt;%26gt; library[count].book.pages;
getline(cfile, library[count].book.genre);
getline(cfile, library[count].book.other.publisher);
cfile %26gt;%26gt; library[count].book.other.year;
}
And I asked this guy and he said:
After you cin%26gt;%26gt; name.first and name.last, you will have read "Jk" and "Rowling", respectively, but you've left the newline in the stream. The call to getline will receive an empty line, and when you try to convert that empty string to a number, you get something completely random like 211400.
How do I get rid of the newline character, something to do with cin.ignore????
How do I get rid of the newline character in C++ using ignore?
The guy you asked answered correctly. When reading from an instream object using %26gt;%26gt;, you have to be careful about newlines being left behind.
I could type up the solution here, but someone else has already done so. I'll give you the link: http://www.daniweb.com/tutorials/tutoria...
I'll summarize the two solutions you have. The first, and recommended for beginners to C++, is to avoid cin and use getline instead. The other way is cin.ignore(numeric_limits%26lt;streamsize%26gt;::m... '\n').
Please read the entire article. It is technically accurate as well.
In the future, use Google to find out answers. There's good resources at "C++ FAQ", cplusplus.com , cppreference.com.
Reply:cin.ignore('\n');
Subscribe to:
Posts (Atom)