| | |
Array troubles?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
•
•
•
•
Originally Posted by marinme
And... they are having you do this without learning about pointers??? Well, if they haven't explained pointers, you probably shouldn't use them.
•
•
•
•
ok, if it's going to be in a class, why not make them data members of the class and just omit declaring them in the function or passing them to the function, just use them from the class..
•
•
•
•
I'm not sure what you mean calling them into the function by an int... do you mean passing them as arguments for the function as an int type? If so, this won't be the best way, you should send them as you are getting them - type char.
•
•
•
•
Any other questions?
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
ok, i read something about passing arrays and ive come to this point in these sections of the prgram
and my main program is
my question here is this...
after i input the info string the first time... I believe it goes right into sortArray and does its thing...
but my program ends suddenly as soon as i input things when its run.
Also if i type quit right away, i get an error message from windows and it automatically closes like all programs do with error messages. Where is this problem? I feel i am array illiterate as i work more and more on this.
C++ Syntax (Toggle Plain Text)
void sortArray(string info[], int v, string course[], char grades[], int hours[]) //use sortArray to sort main input array info { //into three seperate arrays to return string first; //info back for a table first = info[v]; for(int i = 0, j = 0; i< 3 ; i++) { while(first[j] != ' ') { course[j] = first[j]; j++; } j++; //skips space while(first[j] != ' ') { grades[j] = first[j]; j++; } j++; //skips space while(first[j] != ' ') { hours[j] = first[j]; } } }
and my main program is
C++ Syntax (Toggle Plain Text)
int main(){ int hours[50], a = 0, b = 0, c = 1, v, totalHours = 0, gpaSum = 0; char grades[50], gpaForOneClass[50], x; string course[50], info[50], z, schedule[50]; float gpa = 0.0; gradevalue in; cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl; cin>>info[a]; //start of input while (info[a] != "quit" && info[a] != "QUIT") //ends input when quit or QUIT is typed { sortArray(info, v, course, grades, hours); in = convert(in, grades[a]); //converts letter input into right value gpaForOneClass[a] = grade(in, hours[a]); //gets gpa total for one class op_grade(in); //outputs grade letter gpaSum = gpaSum + gpaForOneClass[a]; //totals up gpa totalHours = totalHours + hours[a]; //totals up hours a = a + 1; cout<<"\nEnter the next course, the letter grade received, and the credit hours. "<<endl; cin>>info[a]; // input the next string of info } gpa = gpaAvg(gpaSum, totalHours); //calls gpa function bsort(info, c); // calls bsort function header(); //calls header of table for (b = 0; b < a; b++) //puts the information into the table { cout<<course[b]<<" "<<hours[b]<<" "<<grades[b]<<endl; } cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl; cin>>a; return 0; }
my question here is this...
after i input the info string the first time... I believe it goes right into sortArray and does its thing...
but my program ends suddenly as soon as i input things when its run.
Also if i type quit right away, i get an error message from windows and it automatically closes like all programs do with error messages. Where is this problem? I feel i am array illiterate as i work more and more on this.
•
•
Join Date: Apr 2005
Posts: 63
Reputation:
Solved Threads: 1
•
•
•
•
my question here is this...
after i input the info string the first time... I believe it goes right into sortArray and does its thing...
•
•
•
•
but my program ends suddenly as soon as i input things when its run.
Also if i type quit right away, i get an error message from windows and it automatically closes like all programs do with error messages. Where is this problem? I feel i am array illiterate as i work more and more on this.
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
•
•
•
•
Originally Posted by marinme
Setup breakpoints along the way or just step through the program to see how it is working. I've never used Dev-C++, but in MSVC++ you are able to see your data during the breakpoints, just make sure they are inputting what you have put in there... Pretty much, when you come up with problems like this, you want to eliminate code until it works fine(should have been compiling along the way anyways), then just add a bit and see if it works like you want to, and repeat. This will help you find what part of the code is being troublesome.
in this part of the code:
C++ Syntax (Toggle Plain Text)
int main(){ int hours[50], a = 1, b = 1, c = 1, v, totalHours = 0, gpaSum = 0; char grades[50], gpaForOneClass[50], x; string course[50], info[50], z, schedule[50]; float gpa = 0.0; gradevalue in; cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl; cin>>info[a]; //start of input while (info[a] != "quit" && info[a] != "QUIT") //ends input when quit or QUIT is typed { bsort(info, c); // calls bsort function sortArray(info, v, course, grades, hours); in = convert(in, grades[a]); //converts letter input into right value gpaForOneClass[a] = grade(in, hours[a]); //gets gpa total for one class op_grade(in); //outputs grade letter gpaSum = gpaSum + gpaForOneClass[a]; //totals up gpa totalHours = totalHours + hours[a]; //totals up hours a = a + 1; cout<<"\nEnter the next course, the letter grade received, and the credit hours. "<<endl; cin>>info[a]; // input the next string of info } gpa = gpaAvg(gpaSum, totalHours); //calls gpa function header(); //calls header of table for (b = 1; b <= a; b++) //puts the information into the table { cout<<course[b]<<" "<<hours[b]<<" "<<grades[b]<<endl; } cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl; cin>>a; return 0; }
if you change the line of:
C++ Syntax (Toggle Plain Text)
sortArray(info, v, course, grades, hours);
the program has you input info as always, after you input info.. it asks you to input more however it will print the line:
Enter the next course, the letter grade received, and the credit hours. "
3 times if you enter in info such as cs110 a 3
2 times if you enter in info such as cs110 a
and only once if you just enter in cs110
when you type quit
it displays information from the header function so that part reads right... but
the output is all messed up... It will print a number followed by an ascii character for a few lines
then it prints out the output for this line:
C++ Syntax (Toggle Plain Text)
cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl;
however on that line.. the gpa is always 0 and the hours are some very very high number... usually in the millions.
•
•
•
•
What error message are you getting?
C++ Syntax (Toggle Plain Text)
sortArray(info, v, course, grades, hours);
when you type in any info the program immediately closes as before
when you type quit... the window pops up that says grades.exe has encountered a problem and needs to close. we are sorry... just like it always does for any error a program experiences in windows and then lets you report it or not.
![]() |
Other Threads in the C++ Forum
- Previous Thread: minor problem with vectors and OOP
- Next Thread: error checking of user input
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





