I have to display this prrogram 3 different times using a FOR LOOP but he never showed us how it goes or where it goes I read it in my book and try many different things but nothing works its like it has to display three different people's name and everything using using the FOR LOOP. I am bugging out staying up all night tring to figure this out PLEASE HELP ME PLEASSSSSE!!
I did this program by myself adding the if statement and cin.get but he got me on the rest

// description: display student name, major, status, gpa, and tuition


#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <stdlib.h>

void getdata();
void processdata();
void putdata();

char st_name[25];
char st_major[25],c;
char st_status[9];
float f_gpa;
int i_tuition1;


void main()
{
        getdata();
        processdata();
        putdata();
}
void getdata()
{
        cout << "\n\nPlease enter student name: ";
        cin.getline(st_name,25);

        cout << "\n\nPlease enter student gpa: ";
        cin >> f_gpa;

        cout << "\n\nPlease enter student major: ";
        cin.getline(st_major,25);

        int i=0;
        while((c = cin.get()) != '\n')
{
        st_major[i] = c;
        i++;
}
        st_major[i] = '\0';

        cout << "\n\nPlease enter student status as"
             << " 'I' for instate or 'O' for outstate: ";
        cin.getline(st_status,9);

        cout << "\n\nPlease enter student tuition: ";
        cin >> i_tuition1;
}
void processdata()
{
        if(strcmp(st_status,"outstate")==0)
                i_tuition1= i_tuition1 + 1500;
        if(strcmp(st_status,"instate")==0)
                i_tuition1= i_tuition1 + 0;
}
void putdata()
{
        cout << "\n\nName: " << st_name;
        cout << "\n\nMajor: " << st_major;

        cout << setiosflags(ios::fixed)
             << setiosflags(ios::showpoint)
             << setprecision(2);

        cout << "\n\nGPA: " << f_gpa;
        cout << "\n\nStatus: " << st_status;
        cout << "\n\nTuition: $ " << i_tuition1 << endl;
}

you all can email me @ simmons_hs@hotmail.com

Edited 3 Years Ago by Dani: Formatting fixed

It sounds like you want to change this:

void main()
{
getdata();
processdata();
putdata();
}

To this?

void main()
{
    for (i = 0; i < 3; i++)
    {    
        getdata();
        processdata();
        putdata();
    }
}

And maybe there should be some way to ask how many students to enter, instead of fixing it at 3. But, if 3 is the assignment, then by golly, 3 it shall be! :-)

BTW, is the 'st_major' string entered twice? Looks like you enter it and then enter it again in your while loop. That doesn't look right.

Good luck, and welcome to the forum!

Oh, and before Naru can point out my laziness, I should have declared 'i' before using it. However, let me give you the disclaimer that my suggestions are intended to be SUGGESTIONS and not working/tested code.

Greetings,

Creating a program to loop isn't hard what so ever. In fact, the for loop can help us do this. We have two choices of how we can loop the program over again three times:

  • A static for loop that goes three times
  • Infinite loop, and stop after the third time

Let me explain both.

Static for loops
The for statement contains three components which are expressions. Most commonly expr1 and expr3 are assignments or function calls while expr2 is a relational expression.

for (expr1; expr2; expr3)
	statement

Infinite for loop
Any of the three parts can be omitted, although the semicolons must remain. If expr1 or expr3 is omitted, it is simply dropped from the expansion. If the test, expr2, is not present, it is taken as premanetly true so in this case the following example is an "infinite" loop; presumably to be broken by other means such as break or return.

for (;;) {
	...
}

In example
Lets take your program for example. We will loop everything main() does 3 times.

int main() {
	int i;
	for(i = 0; i < 3; i++) {
		getdata();
		processdata();
		putdata();
	}

	return 0;
}

Code 1.1: Using a static for loop

Now that should make sense. We take in consideration that we are creating a loop to (a) start at 0 (b) express that we are below 3 and (c) increment.

On the other hand, we can use an infinite for loop.

int main() {
	int i=0;
	for(;;) {
		getdata();
		processdata();
		putdata();
		i++;
		if (i > 2)
			break;
	}

	return 0;
}

Code 1.2: Using an infinite for loop

This is also simple to understand. We loop infinitely as i increments, and it does not equal 3 or is not greater than 3; but if it is, break our loop.

If you have further questions, please feel free to ask.


Hope this helps,
- Stack Overflow

Just FYI on the assumption that your teacher is an arrogant fool, like most of them seem to be:

>#include <iostream.h>
>#include <iomanip.h>
These are not standard headers. You're probably using a compiler that is very old, so don't be surprised if you see something completely different from what you're used to as an example here.

>char st_name[25];
>char st_major[25],c;
>char st_status[9];
>float f_gpa;
>int i_tuition1;
Global variables should be avoided. You know how to write functions, so you should know how to pass arguments to them.

>void main()
This is where most teachers insist on being stupid. main returns int, nothing else. This is stated in the C++ standard in no uncertain terms. There is no argument in favor of using void main and plenty of proof that it is a very Bad Thing(TM).

>int i=0;
>while((c = cin.get()) != '\n')
>{
>st_major = c;
>i++;
>}
>st_major = '\0';
I notice that st_major is an array of 25 characters. What happens if the user types 25 characters or more? I can guarantee that it isn't a good thing.

This article has been dead for over six months. Start a new discussion instead.