•
•
•
•
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
![]() |
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Rep Power: 5
Solved Threads: 0
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
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
•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 9
It sounds like you want to change this:
To this?
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!
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!
•
•
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation:
Rep Power: 5
Solved Threads: 9
•
•
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation:
Rep Power: 5
Solved Threads: 4
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:
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.
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.
In example
Lets take your program for example. We will loop everything main() does 3 times.
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.
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
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)
statementInfinite 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; }
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; }
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
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.
>#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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- New to C++ and program (C++)
- "enter" event (C#)
- Browser cannot access internet, but other programs can (Web Browsers)
- More help needed (C++)
- Output in the text file (C)
Other Threads in the C++ Forum
- Previous Thread: Execute from Sparc Solaris, retrieve a recordset from VB application running on WIN2K
- Next Thread: Alex Cavnar - rectangle problem not solved



Linear Mode