User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 425,982 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,652 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1457 | Replies: 4
Reply
Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Someone Be My Program Study Buddy

  #1  
Oct 7th, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 9
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Someone Be My Program Study Buddy

  #2  
Oct 7th, 2004
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!
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 9
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Someone Be My Program Study Buddy

  #3  
Oct 7th, 2004
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.
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Someone Be My Program Study Buddy

  #4  
Oct 7th, 2004
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Sep 2004
Posts: 6,324
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Someone Be My Program Study Buddy

  #5  
Oct 7th, 2004
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[i] = c;
>i++;
>}
>st_major[i] = '\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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC