Help me, fix this simple program
Sep 11, 2017 at 11:15pm UTC
Guys, how i can use space in string?
try this code lest you know
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
main()
{
char indeks[10];
string name[100];
int amount;
int score[10];
cout<<"Enter the number of students :" ;
cin>>amount;
for (int i=1;i<=amount;i++)
{
cout<<"Name:" ;
getline(cin,name[i]);
cout<<"Score :" ;
cin>>score[i];
if ((score[i]<=100)&&(score[i]>80))
{
indeks[i]='A' ;
}
else if ((score[i]<=79)&&(score[i]>=60))
{
indeks[i]='B' ;
}
else if ((score[i]<=59)&&(score[i]>=40))
{
indeks[i]='C' ;
}
else if ((score[i]<=39)&&score[i]>=10)
{
indeks[i]='D' ;
}
else
{
indeks[i]='E' ;
}
}
for (int j=1;j<=amount;j++)
{
cout<<"Studen Name: " <<name[j]<<endl;
cout<<"Score: " <<score[j]<<endl;
cout<<"Indeks : " <<indeks[j]<<endl;
}
getch();
}
Sep 12, 2017 at 2:52am UTC
@irmanfreestyle
Your going to be out of bounds in the indexs and score arrays, if the user picks an amount on line 13 that is 9 or higher since you've declared those two as 10, which is 0 to 9.
Also, could you explain what you mean by
how i can use space in string?
?
Sep 12, 2017 at 3:42am UTC
@whitenite1
Sory bad english,
Why can not I enter a student name?
name input is not executed and straight to age
getline (cin, name)
Sep 12, 2017 at 4:09am UTC
@irmanfreestyle
Add cin.sync();
as a line right after line 16 to clear the input buffer. And you may want to raise all your arrays to be 100, so you don't go out of bounds on your arrays.
Sep 12, 2017 at 7:31am UTC
@irmanfreestyle
Use vectors instead. It's much better than simple arrays.
Sep 12, 2017 at 8:11am UTC
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <iostream>
#include <string>
// #include<conio.h> <-- conio.h is not standard: better avoided
// using namespace std; <-- do you really need this?
int main()
{
std::cout << "Enter the number of students (max 10): " ;
int amount {};
std::cin >> amount;
std::cin.ignore(1); // ignore the '\n' after the number
char indeks[10];
std::string name[10];
int score[10];
for (int i=0; i<amount; i++) // arrays start from 0
{
std::cout << "Name: " ;
std::getline(std::cin, name[i]); // <-- first position is name[0]
std::cout << "Score (0-100): " ;
std::cin >> score[i];
std::cin.ignore(1); // ignore the '\n' after the number
if (score[i]>80)
{
indeks[i] = 'A' ;
}
else if ((score[i]<=79) && (score[i]>=60))
{
indeks[i]='B' ;
}
else if ((score[i]<=59) && (score[i]>=40))
{
indeks[i]='C' ;
}
else if ((score[i]<=39) && score[i]>=10)
{
indeks[i]='D' ;
}
else
{
indeks[i]='E' ;
}
}
for (int j=0; j<amount; j++)
{
std::cout << "Student name: " << name[j]
<< "; Score: " << score[j]
<< "; Indeks: " << indeks[j] << '\n' ;
}
// getch(); <-- non standard: better avoided
std::cout << "\nPress ENTER to continue...\n" ;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
}
note: to prevent console from closing after code execution:
http://www.cplusplus.com/forum/beginner/1988/
Topic archived. No new replies allowed.