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];
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:
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.
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.
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.
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.
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?
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/
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
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.