Hello all. I'm a first year C++ student and I'm going cross-eyed trying to figure out what's wrong with this code that I've started on. I need a little help understanding parallel arrays.

The program is supposed to let a user input their Student ID. From there, it will either find it and show the student their information OR it will say "not found" if it's not found in the array.

My problem is that any number I input, will only show the first index of the 3 parallel arrays. I was told to use a boolean (found = false or true) but that didn't work either. I've made such a mess, but I cant figure out how to relate the user input to the ID array. Also, I have to figure out how to relate that array to the matching array. I'm pretty sure it's a simple fix but I'm not seeing it at all.

Please help.. *beg*

Thank you for any and all tips that you can give me.

Madison Alexis

#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;

int main() {

        // Array Declarations -- hard code
	int ID[5] = { 1234, 3225, 4413, 6438, 1358};
	float Ex[5] = { 81.21F, 74.53F, 90.50F, 74.81F, 62.31F};
	float Lab[5] = { 83.57F, 82.80F, 84.15F, 93.67F, 72.20F};

	const int n = 5;
	int StudentID;
	bool Found = false;
	float SemGrade;
	char CloseP;
	int k;
	
       // Code to match student ID with input
	for(k = 0; k < n; k++)
     {
	 cout << "Enter your Student ID: ";	
	 cin >> StudentID;
        
	 if(ID[k] == StudentID)     
		Found = true;
	  else
	Found = false;
  cout << "None found";
  	cout << endl;

	//Semester grade calculation
	SemGrade = 0.6*Ex[k] + 0.4*Lab[k];
 
	// Display student information
	cout << "ID: " << StudentID << "\n";
	cout << "Exam Average = " << Ex[k] << "\n";
	cout << "Lab Average = " << Lab[k] << "\n";
	cout << "Semester Grade = " << SemGrade << "\n";

	if (SemGrade >=90 && SemGrade <=100){
        cout << "Letter grade = A" << endl << endl;
        }
    else if (SemGrade >=80 && SemGrade <=89){
        cout << "Letter grade = B" << endl << endl;
        }
    else if (SemGrade >=70 && SemGrade <=79){
        cout << "Letter grade = C" << endl << endl;
        }
    else if (SemGrade >=60 && SemGrade <=69) {
        cout << "Letter grade = D" << endl << endl;
        }
    else {
        cout << "Letter grade = F" << endl << endl;
	}
	// Ends the program.
	cout << "Enter any value to close console.";
	cin >> CloseP;
	return 0;
}
}

Regardless of whether or not it is found, your results section is going to output for the current k, so if k=0, you're going to get index 0 from your other arrays.

Some p-code:

Input the ID
For loop (over all the ids)
   If it exists, note the index of it
      Calculate and display that index of the other arrays
      Break out of the for loop
   Else
      Go back to the Input the ID step

Thanks for replying Jonsca. :)

I've been trying to decipher your pseudocode and so far, I've managed to ruin my program even more. >.<

This is what I have so far from what you've said.

cout << "Enter your Student ID: ";	
	cin >> StudentID;
	for(k = 0; k < n; ++k)
	cin >> k;
	{
	if (StudentID == k)

		cout << "Your ID: " << StudentID << "\n";	
        }
	else
	cout << "Enter your Student ID: ";	
	cin >> StudentID;

It's not even displaying the student information anymore. I'm a little (a lot) lost.
I tried using k this time to note the index. But so far, no go. I'm currently reading a self-teaching book, (C++ DeMystified) but it doesn't even have a page for parallel arrays that I can reference. Also, google is not my friend today. lol.

A little more help please?

Madison Alexis

Not what one would call a program....but it works

#include <stdafx.
#include <iostream>
#include <string>
using namespace std;
 
int main() {
 
        // Array Declarations -- hard code
	int ID[5] = { 1234, 3225, 4413, 6438, 1358};
	float Ex[5] = { 81.21F, 74.53F, 90.50F, 74.81F, 62.31F};
	float Lab[5] = { 83.57F, 82.80F, 84.15F, 93.67F, 72.20F};
 
	int StudentID;
	float SemGrade;
	int k;
    int index;
    int i;
    char response;

        start:
        cout << "Enter your Student ID: ";
        cin >> StudentID;

        while(i<5)
        {
           if(StudentID == ID[i])
           {
             k = i;
             goto calc;

           }
           else
           {
             i++;
           }
        }
        cout << "\nNo Match Found!!\n\n";
        goto get_response;

        calc://Semester grade calculation
	    SemGrade = 0.6*Ex[k] + 0.4*Lab[k];
 
	// Display student information
	cout << "\nID: " << StudentID << "\n";
	cout << "Exam Average = " << Ex[k] << "\n";
	cout << "Lab Average = " << Lab[k] << "\n";
	cout << "Semester Grade = " << SemGrade << "\n";
 
	if (SemGrade >=90 && SemGrade <=100){
        cout << "Letter grade = A" << endl << endl;
        }
    else if (SemGrade >=80 && SemGrade <=89){
        cout << "Letter grade = B" << endl << endl;
        }
    else if (SemGrade >=70 && SemGrade <=79){
        cout << "Letter grade = C" << endl << endl;
        }
    else if (SemGrade >=60 && SemGrade <=69) {
        cout << "Letter grade = D" << endl << endl;
        }
    else {
        cout << "Letter grade = F" << endl << endl;
	     }

    get_response:
    cout << "Press y to continue or any other key to exit : ";
    cin >> response;
    cout << endl;
    if(response == 'y' || response == 'Y')
    {
      i=0;
      goto start;
    }
    else
    {
      exit(1);
    }

	return 0;

}
commented: Do not do this -1
commented: Yuk. -3

Look at line 4 in your code. Do you want to be taking in input for each element of your array? Doesn't make too much sense. Notice I had input the ID outside of the for loop.

Think about setting the whole thing up in a do/while loop, where the end result is going back to the beginning.

Roughly:

do {
   cout<<"Enter your student ID: ";
   cin >> id; //don't use k for this too

   for (int i = 0;i<n;++i)
   {
       if(StudentID[i] == k)
       {
            //do all your other stuff here using this i
            break; //since you are done with the loop once you've found it
       }

       //else we keep going through the loop
//prompt the user if they want to go around again and use that in the condition
//in the while loop
}while( condition here);

I wouldn't worry about ruining your program hehe. There aren't many among us that haven't scrapped large sections of code from time to time.

See if that gives you some leads. If you have to, take out all of the other calculations and displays for now and start with finding if the ID is in the set, build from there.

No, that's not the right thing Lanor. I am perfectly capable of giving the OP a working program for this problem. He/she won't learn anything the way you have done it. (Besides, never use goto)

commented: Sorry... my mouse accidentally click on -1.. Really sorry. +6

Thanks again for replying....

I'm still trying to figure it out. Thanks Lonar for your code. It might help me out if I look at it but go_to code isn't in my self-teaching book. My head hurts now. I think I'm going to give up until tomorrow. The errors are killing me and there's no way to see what's wrong until the errors are fixed. This program wasn't so hard when I did wrote it in BASIC. I don't know why I'm having a hard time translating it to C++. <3

Question before I totally give up for today:

When you wrote this:

#
cout<<"Enter your student ID: ";
#
cin >> id; //don't use k for this too

Is that id different from ID (the array?) Meaning, do I have to declare that also?

Anyway, you have my gratitude.

thank you for the reminder,Jonsca.I never doubted your capabilities.
Looks like Madilexi has tried real hard. Heres my version of your program(without goto statements and similar to yours).Not a good program I'd say, but it works.
See if you can figure out whats happening. Do not copy code, you will not learn from copying(wise words from Jonsca).

#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;

int main() {

    // Array Declarations -- hard code
	int ID[5] = { 1234, 3225, 4413, 6438, 1358};
	float Ex[5] = { 81.21F, 74.53F, 90.50F, 74.81F, 62.31F};
	float Lab[5] = { 83.57F, 82.80F, 84.15F, 93.67F, 72.20F};

	int StudentID;
	bool Found = false;
	float SemGrade;
	int k = 0;
	string response = "y";
	

    do{
	    cout << "Enter your Student ID: ";
	    cin >> StudentID;

         /*
	while loop checks your ID array for a match with your input.
	Note variable k is initialized to zero.
	Checks ID array for a match. if match is found, sets Found to true and breaks out of while loop. 
         If no match is found at index k,it increments k by 1 and checks array ID at index k for a match.
	Loop terminates after 5 iterations and if no match was found, Found remains false.   
        */
        while(k<5)//5 used here because your ID array has 5 elements.
        {
            if(StudentID ==ID[k])
            {
                Found=true;
                break;//break out of while loop

            }
            else
            {
                k++;// increase k to check kth index of your ID array
            }
        }

        //program execution here after breaking out of loop or after end of while loop after 5 iterations.
        
		if(Found==true)//executed if found was set to true, thats if match was found
        {
           //Semester grade calculation
	        SemGrade = 0.6*Ex[k] + 0.4*Lab[k];
 
            cout << endl;
	        
	   if (SemGrade >=90 && SemGrade <=100)
            {
                cout << "Letter grade = A" << endl << endl;
            }
            else if (SemGrade >=80 && SemGrade <=89)
            {
                cout << "Letter grade = B" << endl << endl;
            }
            else if (SemGrade >=70 && SemGrade <=79)
            {
                cout << "Letter grade = C" << endl << endl;
            }
            else if (SemGrade >=60 && SemGrade <=69)
            {
                cout << "Letter grade = D" << endl << endl;
            }
            else
            {
                cout << "Letter grade = F" << endl << endl;
            }
			
	   // Display student information
            cout << "ID: " << StudentID << "\n";
	   cout << "Exam Average = " << Ex[k] << "\n";
	   cout << "Lab Average = " << Lab[k] << "\n";
	   cout << "Semester Grade = " << SemGrade << "\n";
        }
        else//executed if found was false
        {
            cout << "\nNo Match Found with ID " << StudentID;
        }

        cout << "\n\nPress y to continue or any other key to exit: ";
        cin >> response;
        cout << endl;

        if(response != "y")
        {
            exit(1);//exit if response is not y
        }
        else
        {
            k=0;//set k to 0(for while loop)
            Found = false;
        }
     }while(response == "y");

	return 0;
}

Yes! I figured the array out! Thanks so much for your help, Jonsca. Everything you said makes sense. I had to re-write a few things from scratch, but the array part works now perfectly.

I cannot believe a little tiny code k=i was throwing me off so badly for 3 days straight.

Now, all i need to do is figure out how to get the program to say "sorry no ID found" and Press any key to close the console, when it doesn't find the ID in the array, instead of looping back to inputting the ID.

Also, I'm going to re-write the letter grade thing b/c the If and else statement makes the coding look a bit cluttered.

Madison Alexis <3

commented: Excellent, good effort! +16

Thanks Lonar. I did it a little weird.

do {
		cout << "Enter your ID: ";
		cin >> StudentID;
		cout << endl;

		for (int i=0; i<n;++i)
			if(ID[i] == StudentID)
		{
			K = i;

			SemGrade = 0.60F*Ex[i] + 0.40F*Lab[i];		
			cout << "Your ID: " << StudentID << "\n";
			cout << "Your Exam Average = " << Ex[i] << "\n";
			cout << "Your Lab Average = " << Lab[i] << "\n";
			cout << "Semester Grade = " << SemGrade << "\n";
			if (SemGrade >=90 && SemGrade <=100){
		   cout << "Letter grade = A" << endl << endl;
        }
		else if (SemGrade >=80 && SemGrade <=89){
        cout << "Letter grade = B" << endl << endl;
        }
    else if (SemGrade >=70 && SemGrade <=79){
        cout << "Letter grade = C" << endl << endl;
        }
    else if (SemGrade >=60 && SemGrade <=69) {
        cout << "Letter grade = D" << endl << endl;
        }
    else {
        cout << "Letter grade = F" << endl << endl;
	}

			Found = true;
			}

It works except it loops back when it doesn't find the user input in the array.

I think I'll get some more sleep and finish this thing tomorrow. Thanks everyone. :)

Thats great Madilexi. I never had used bool when writing programs.Thats because I never understood it. Thanks to you, I learned bool today while attempting your problem.
Good Luck.

Do not copy code, you will not learn from copying(wise words from Jonsca)

I learned bool today while attempting your problem.

There's nothing wrong with learning from her exercises, but there's a sure fire way to prevent an OP from copying the code: don't give it out.

Yes! I figured the array out! Thanks so much for your help, Jonsca

Excellent! I had to attend to other things but great that you got it working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.