how do i get my information on multiple lines

I'm doing an assignment which is supposed to have first, last name and GPA. I can get everything to print on one line but I need them on there own separate lines. such as

First, last name GPA
Bill, someone 4
Joey, Someone 3


I'm also trying to get it to display the first name, last name GPA titles as well. probably an easy fix but I'm stuck ive also tried endl; and \n after each. if there is away to shrink this down from how long I have it, I'm open to your ideas.

#include <iostream>
#include <string>

int main()
{
//first person//
std::string names[20];
std::cout<<"enter your first name, last name :";
std::cin>>names[0];
std::cout<<"enter student GPA :";
std::cin>>names[1];


std::cout<<"enter your first name, last name :";
std::cin>>names[2];
std::cout<<"enter student GPA :";
std::cin>>names[3];


std::cout<<"enter your first name, last name :";
std::cin>>names[4];
std::cout<<"enter student GPA :";
std::cin>>names[5];


std::cout<<"enter your first name, last name :";
std::cin>>names[6];
std::cout<<"enter student GPA :";
std::cin>>names[7];


std::cout<<"enter your first name, last name :";
std::cin>>names[8];
std::cout<<"enter student GPA :";
std::cin>>names[9];


std::cout<<"enter your first name, last name :";
std::cin>>names[10];
std::cout<<"enter student GPA :";
std::cin>>names[11];


std::cout<<"enter your first name, last name :";
std::cin>>names[12];
std::cout<<"enter student GPA :";
std::cin>>names[13];


std::cout<<"enter your first name, last name :";
std::cin>>names[14];
std::cout<<"enter student GPA :";
std::cin>>names[15];


std::cout<<"enter your first name, last name :";
std::cin>>names[16];
std::cout<<"enter student GPA :";
std::cin>>names[17];


std::cout<<"enter your first name, last name :";
std::cin>>names[18];
std::cout<<"enter student GPA :";
std::cin>>names[19];




//ouput of people//

std::cout << names[0] << " " << names[1];

std::cout << names[2] << " " << names[3];

std::cout << names[4] << " " << names[5];

std::cout << names[6] << " " << names[7];

std::cout << names[8] << " " << names[9];

std::cout << names[10] << " " << names[11];

std::cout << names[12] << " " << names[13];

std::cout << names[14] << " " << names[15];

std::cout << names[16] << " " << names[17];

std::cout << names[18] << " " << names[19];

return 0;

}
Have you heard of loops? srs question.
I have I haven't been in a c++ course for almost a year now so I don't remember much of how to do it. I'm studying to do cyber security but prereq I have to do these couple of c++ classes.
So you'll need a string array to to hold first names, a string array to hold last names and double array to hold the GPA. All three arrays will be of size 20 like so:

1
2
3
4
const int SIZE = 20;
std::string firstNames[SIZE];
std::string lastNames[SIZE];
double gpa[SIZE];


Now that you've created all three arrays, you need to fill them up with data. Use a loop to do this:

1
2
3
4
5
6
for(int i = 0; i < SIZE; i++)
{
    cin >> firstNames[i];
    cin >> lastNames[i];
    cin >> gpa[i];
}


The above code will fill up all three arrays. Of course, you should cout some prompts so the user knows what to enter.

Now that the arrays are filled up, use a similar loop to cout their contents.

1
2
3
4
5
for(int i = 0; i < SIZE; i++)
{
    cout << firstNames[i] << ", " << lastNames[i] << " " << gpa[i] << "\n";
   // the "\n" at the end there will move the next output to a new line. 
}


This may not be the best way to do this, but its fine for you.
Last edited on
The last piece with cout should that go before the cin portion to declare the information needed, I'm assuming this is how this works.
No. It will go after.

cin is for user input. cin >> firstNames[i] means that whatever string the user entered will be stored in the i-th position in the array 'firstNames'.
Same is true for all the other cins.

cout is output. cout << firstNames[i] means whatever is stored in the i-th positions of the array 'firstNames' will be printed onto the screen.

Notice that both are in a loop, thus "i" will change, moving from 0 to 19.
Last edited on
I pretty sure I'm screwing this up but it is asking for the correct information several times but is only printing the first set of information once.


#include <iostream>
#include <string>

int main()
{
const int SIZE = 20;
std::string firstNames[SIZE];
std::string lastNames[SIZE];
double gpa[SIZE];

for(int i = 0; i < SIZE; i++)
{
std::cout <<"Firstname";
std::cin >> firstNames [i];
std::cout <<"Lastname";
std::cin >> lastNames [i];
std::cout <<"GPA";
std::cin >> gpa [i];
}


for(int i = 0; i < SIZE; i++)
{
std::cout << firstNames[i] << ", " << lastNames[i] << " " << gpa[i] << "\n";





return 0;
}
Last edited on
Put in the closing brace for the second for-loop.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

int main()
{
    const int SIZE = 2;
    std::string firstNames[SIZE];
    std::string lastNames[SIZE];
    double gpa[SIZE];

    for(int i = 0; i < SIZE; i++)
    {
        std::cout <<"Firstname";
        std::cin >> firstNames [i];
        std::cout <<"Lastname";
        std::cin >> lastNames [i];
        std::cout <<"GPA";
        std::cin >> gpa [i];
    }

    for(int i = 0; i < SIZE; i++)
    {
        std::cout << firstNames[i] << ", " << lastNames[i] << " " << gpa[i] << "\n";
    }

    return 0;
}
FirstnameBob
LastnameMoat
GPA2
Firstnameclive
LastnameIndia
GPA3
Bob, Moat 2
clive, India 3


It's your lucky day. In future please use code tags <> and properly indent it, tidy it up by removing unnecessary blank lines, instead of presenting it as a dog's breakfast, all of which helps you in debugging your code. :)
I was looking at other options and put some different combinations toghther and it works, but if someone could explain whats going on a little better to me that would be great. I get what cin and cout are but like int and string I'm still kind of confused how they get me where they got me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;

int main()
{
   int numstudents;
   cout << "Enter the class size: ";
   cin >> numstudents;
   string first, last;
   string names[numstudents];
   int GPA[numstudents];

   for (int i = 0; i < numstudents; i++) {
      cout << "Enter a student's name First, last: ";
      cin >> first >> last;
      names[i] = string(first) + string(" ") + string(last);
      cout << "Enter " << names[i] << "'s GPA: ";
      cin >> GPA[i];
   }

   cout << "\n\nStudents name:      " << "    GPA:"<<endl;
   for (int j = 0; j < numstudents; j++) {
      cout << "" << names[j] << "          " << GPA[j]<< endl;
     
   }

   return 0;
}

Last edited on
My new question is everything sits where I want it but if one names longer than the other it will sit more to the side than the others. for example


student__________________GPA
Jack man_________________4
James someone_______________3

this is what the end product looks like is there away to keep them aligned?
I put underscores because this block keeps aligning things and I want people to see what I see.
Last edited on
Kemort I have a couple more questions for you. first how do I get it to except a GPA standard like 4.0 or 5.57 stuff like that and second question I use DEV C++ and when I try to use the debugger it gives me a popup saying (you have not enabled debugging info (-g) and / or stripped it from the executable (-s) in compiler option) how do I fix this?
closed account (48T7M4Gy)
Avoid dev c++ and use an up to date, modern (free too) IDE such as codeblocks, vstudio, xcode etc etc. However if you don't want the popup appearing it might be a good idea to do what it says :)

Try the tutorial http://www.cplusplus.com/doc/ It's extraordinarily well adapted to your situation and for ongoing reference and help to supplement your (welcome) efforts here.

One aspect you also need to master here is code tags and to that end you might also like to consult http://www.cplusplus.com/forum/articles/42672/

I figured one thing out but is there away to keep the GPA aligned its not a big deal at this point I appreciate the help.
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/iomanip/setw/?kw=setw
Thanks for the help kemort. I decided to download the code blocks IDE. is there a compiler that you would suggest I use for the latest version of code blocks? its code blocks 16.01
Last edited on
closed account (48T7M4Gy)
From memory codeblocks package comes with cygwin and/or mingw which work OK if you can come to grips with the path configuration etc. Perserverance pays off, but CB is good overall.
happy85, I think everyone is overthinking this. Tell me if I am wrong, but it seems that you forgot and endl (endline) statement at the end or your statements.

In order to display the information like so:

First, last name GPA
Bill, someone 4
Joey, Someone 3

This is what you should put:
1
2
3
4
5
6
7
8

int main() 
{
     //All you need to do is put "<< endl;" at the end of the cout statement.
     cout << (whatever needs to be on the line) << endl;
}



I hope that helps. :)

EDIT:

I am not sure if what I said actually has anything to do with your problem, but if it does not, I am sorry.
Last edited on
Topic archived. No new replies allowed.