无法从字符串数组中获取信息 [英] Trouble getting information to and from a string array
本文介绍了无法从字符串数组中获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我唯一的猜测是我应该使用getline(cin,var)而不仅仅是cin但是我玩//并且没有成功。
< span class =code-keyword> #include < cstdlib >
#include < iostream >
#include < iomanip >
#include < string < span class =code-keyword>>
#include < conio.h >
#include < cstring >
使用 命名空间标准;
// 全局常量
const int CLASS = 4 ,
COLS = 4 , // 列中每个数组
ROWS = 4 , // 表格中的行
NEW_GRADE = 24 ;
// 简单的启动画面
void goSplash();
// 是的,七个变量发送菜单功能
void showMenu( const int , const int , const int , const int , const int , const int , int &);
// 我的冒泡排序
void goSort( int [], const INT 跨度>);
int main()
{
/ / 菜单的常量
const int CLASS1 = 1 , // 菜单信息的常量
CLASS2 = 2 ,
CLASS3 = 3 ,
CLASS4 = 4 ,
CLASS5 = 5 ,
CLASS_ALL = 6 ;
// 保留用户选择的值和
int choice;
// 新成绩数组
int newGrade [CLASS];
// 类的字符串数组名称
string classArray [CLASS]; // 类名的字符串数组
// 类百分比的整数2d数组
int tableGrade [ROWS] [COLS];
// 程序介绍暂停和屏幕清晰的基本信息介绍
cout<< \ n此程序将允许您查看和操作您的,\ N跨度>;
cout<< 测试成绩在四个季度的学期。\ n;
cout<< 您将列出四个科目或类别,而不是您的分数。\ n 跨度>;
cout<< 您可以查看您的分数,对它们取平均值或按类别对它们进行排序。跨度><< ENDL;
cout<< \ nPLEASE按ENTER键启动程序<< ENDL;
// 暂停和清除
_getch();
system( CLS);
// 启动画面
goSplash();
// 为类数组添加名称
的类=code-keyword>( int count = 0 ; count< CLASS; count ++)
{
cout<< \ n输入类的名称(<< count + 1<< ):;
cin>> classArray [count];
}
// 这些for循环很容易,没有验证来制作它们大量
// 我需要用百分比填充五行五列
for ( int val = 0 ,num = 0 ; val< COLS; val ++)
{
do
{
cout<< \ n输入得分编号<< num + 1<< 百分之百为< ;< classArray [val]<< :;
cin>> tableGrade [NUM] [VAL];
while (tableGrade [num] [val]< 0 || tableGrade [num] [val]> 100 )
{
cout<< 该值必须介于0和100之间:;
cin>> tableGrade [NUM] [VAL];
cout<< \ n;
}
if (cin.fail())
{
cin。明确();
cin.ignore(INT_MAX,' \ n');
}
} while (cin.fail());
}
for ( int val = 0 ,num = 1 ; val< COLS; val ++)
{
执行
{
cout<< \ n输入得分数<< num + 1<< 百分之百为<< classArray [val]<< :;
cin>> tableGrade [NUM] [VAL];
while (tableGrade [num] [val]< 0 || tableGrade [num] [val]> 100 )
{
cout<< 该值必须介于0和100之间:;
cin>> tableGrade [NUM] [VAL];
cout<< \ n;
}
if (cin.fail())
{
cin。明确();
cin.ignore(INT_MAX,' \ n');
}
} while (cin.fail());
}
for ( int val = 0 ,num = 2 ; val< COLS; val ++)
{
执行
{
cout<< \ n输入得分数<< num + 1<< 百分之百为<< classArray [val]<< :;
cin>> tableGrade [NUM] [VAL];
while (tableGrade [num] [val]< 0 || tableGrade [num] [val]> 100 )
{
cout<< 该值必须介于0和100之间:;
cin>> tableGrade [NUM] [VAL];
cout<< \ n;
}
if (cin.fail())
{
cin。明确();
cin.ignore(INT_MAX,' \ n');
}
} while (cin.fail());
}
for ( int val = 0 ,num = 3 ; val< COLS; val ++)
{
do
{
cout<< \ n输入得分数<< num + 1<< 以百分比表示100的<< classArray [val]<< :;
cin>> tableGrade [NUM] [VAL];
while (tableGrade [num] [val]< 0 || tableGrade [num] [val]> 100 )
{
cout<< 该值必须介于0和100之间:;
cin>> tableGrade [NUM] [VAL];
cout<< \ n;
}
if (cin.fail())
{
cin。明确();
cin.ignore(INT_MAX,' \ n');
}
} while (cin.fail());
}
for ( int val = 0 ,num = ROWS; val< COLS; val ++)
{
do
{
cout<< \ n输入得分数< <<< classArray [val]<< :;
cin>> tableGrade [NUM] [VAL];
while (tableGrade [num] [val]< 0 || tableGrade [num] [val]> 100 )
{
cout<< 该值必须介于0和100之间:;
cin>> tableGrade [NUM] [VAL];
cout<< \ n;
}
if (cin.fail())
{
cin。明确();
cin.ignore(INT_MAX,' \ n');
}
} while (cin.fail());
}
showMenu(CLASS1,CLASS2,CLASS3,CLASS4,CLASS5,CLASS_ALL,选择);
while (选择< 1 || choice> 7 )
{
cout< 请选择1到6之间的选项:;
cin>> choice;
if (choice == 7 )
{
system( PAUSE);
退出;
}
}
// 我决定的大型交换语句发送出2d数组
switch (选择)
{
案例 1 :
cout<< 您的成绩为<< classArray [ 0 ]<< are.\\\
;
for ( int num = 0 ; num< COLS; num ++)
{
cout<< 等级: << num + 1<< tableGrade [num] [ 0 ]<< \\\
跨度>;
}
break ;
case 2 :
cout<< 您的成绩为<< classArray [ 1 ]<< are.\\\
;
for ( int num = 0 ; num< COLS; num ++)
{
cout<< 等级:%<< num + 1<< tableGrade [num] [ 1 ]<< \\\
跨度>;
}
break ;
case 3 :
cout<< 您的成绩为<< classArray [ 2 ]<< are.\\\
;
for ( int num = 0 ; num< COLS; num ++)
{
cout<< 等级: %<< num + 1<< tableGrade [num] [ 2 ]<< \\\
跨度>;
}
break ;
case 4 :
cout<< 您的成绩为<< classArray [ 3 ]<< are.\\\
;
for ( int num = 0 ; num< COLS; num ++)
{
cout<< 等级:<< num + 1<< tableGrade [num] [ 3 ]<< \\\
跨度>;
}
case 5 :
cout< ;< 您的成绩为<< classArray [ 4 ]<< are.\\\
;
for ( int num = 0 ; num< COLS; num ++)
{
cout<< 等级:<< num + 1<< tableGrade [num] [ 4 ]<< \\\
跨度>;
}
// 循环酷的副作用。将我的2d转储到一维数组而不是将它们发送到排序函数
case 6 :
cout<< 您班级的排序等级为。\ n 跨度>;
for ( int var = 0 ,big = 0 ; var< COLS; var ++,big ++){newGrade [big] = tableGrade [ 0 跨度>] [变种]; }
for ( int var = 0 ,big = 5 ; var< COLS; var ++,big ++){newGrade [big] = tableGrade [ 1 跨度>] [变种]; }
for ( int var = 0 ,big = 10 ; var< COLS; var ++,big ++){newGrade [big] = tableGrade [ 2 跨度>] [变种]; }
for ( int var = 0 ,big = 15 ; var< COLS; var ++,big ++){newGrade [big] = tableGrade [ 3 跨度>] [变种]; }
for ( int var = 0 ,big = 20 ; var< COLS; var ++,big ++){newGrade [big] = tableGrade [ 4 跨度>] [变种]; }
// 对大等级表进行排序
goSort( newGrade,NEW_GRADE);
break ;
默认:
cout<< 输入错误无效输入<< endl;
break ;
}
cout<< 您是否希望立即退出该计划\\\
跨度>;
cout<<
system( PAUSE);
退出;
}
// while循环递减一些符号
void goSplash()
{
int count = 1000 跨度>;
while (count> 0 )
{
count- - ;
cout<< 101101111100000001101010010100100100111110010101001010101110101;
if (count%5 == 0 )
{
cout<< %^%& ^(^&(^ *%^%* %% ^ $ (^&%^ $ * $ * $%* $%*%**%*;
}
}
cout<< setw( 50 )<< CHRIS'S GRADE KEEPER ;
system( CLS);
}
void showMenu( const int CLASS1, const int CLASS2, const < span class =code-keyword> int CLASS3, const int CLASS4, const int CLA SS5, const int CLASS_ALL, int & ;选项)
{
const int EXIT = 7 跨度>;
cout<< ***** MENU ***** \ N跨度>;
string menuStar = *****;
for ( int menu = 50 ; menu> 5 ; menu - = 5 )
{
COUT<< menuStar<< ENDL;
}
cout<< \ n;
cout<< CLASS1<< - 显示第一堂课得分\ n< /跨度>;
cout<< CLASS2<< - 显示第二课程成绩\ n< /跨度>;
cout<< CLASS3<< - 显示第三课成绩\ n< /跨度>;
cout<< CLASS4<< - 显示第四课程成绩\ n< /跨度>;
cout<< CLASS5<< - 显示第五课程成绩\ n< /跨度>;
cout<< CLASS_ALL<< - 显示排序的所有课程分数\ n 跨度>;
cout<< EXIT<< - 退出程序\ n \ n 跨度>;
cout< 请选择1到7之间的选项:;
执行 {
cin>> choice;
if (choice == 7 )
{
system( pause);
退出( 0 );
}
} while (choice == 7 );
}
void goSort( int newGrade [], const int NEW_GRADE)
{
的类=code-keyword>( int k = 1 ; k< NEW_GRADE; k ++)
for ( int i = 0 ; i< new_grade - 1 = - = mode = hold /> if (newGrade [i]> newGrade [我+1]){
int temp = newGrade [i];
newGrade [i] = newGrade [i + 1 ];
n ewGrade [i + 1 ] = temp;
}
for ( int count = 0 ; count< NEW_GRADE; count ++)
{
cout<< 成绩<< count<< :< < newGrade [count]<< \ n;
}
}
解决方案
(^&%^
*
*
My only guess is I should be using getline(cin, var) instead of just cin but i played with //it and had no success .
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
#include <cstring>
using namespace std;
// Global constants
const int CLASS = 4,
COLS = 4, //columns in each array
ROWS = 4, //rows in table
NEW_GRADE = 24;
//simple splash screen
void goSplash();
//yes that is seven variables sent the menu function
void showMenu( const int , const int, const int ,const int ,const int ,const int ,int &);
//my bubble sort
void goSort(int[] , const int);
int main()
{
//constants for the menu
const int CLASS1 = 1,//constants for menu information
CLASS2 = 2,
CLASS3 = 3,
CLASS4 = 4,
CLASS5 = 5,
CLASS_ALL = 6;
//hold the value of the choice for the user and
int choice;
//new grades arrays
int newGrade[CLASS];
//string array for class names
string classArray[CLASS];//string array for class names
//integer 2d array for class percentages
int tableGrade[ROWS][COLS];
//program intro a basic information intro with a pause and a screen clear
cout <<"\nThis program will allow you to view and manipulate your,\n";
cout<<"test scores in in four quarter semester.\n";
cout<<"Your will list four subjects and or classes and than your scores.\n";
cout<<"Your can view your scores, average them or sort them by class."<<endl;
cout<<"\nPLEASE PRESS ENTER TO START PROGRAM"<<endl;
//pause and clear
_getch();
system("CLS");
//splash screen
goSplash();
//add names to the classes array
for (int count = 0; count < CLASS; count++)
{
cout <<"\nEnter the name of class ("<<count + 1<<") : ";
cin>>classArray[count];
}
//these for loops where easy without the validation to make them massive
//I needed to fill five rows and five columns with percentages
for (int val = 0, num = 0; val < COLS; val ++)
{
do
{
cout <<"\nEnter score number "<<num +1<<" in percent out of 100 for "<< classArray[val]<<" : ";
cin >> tableGrade[num][val];
while (tableGrade[num][val] < 0 || tableGrade[num][val] > 100)
{
cout<<"The value must be between 0 and 100: ";
cin >> tableGrade[num][val];
cout<<"\n";
}
if (cin.fail())
{
cin.clear();
cin.ignore(INT_MAX, '\n');
}
}while (cin.fail());
}
for (int val = 0, num = 1; val < COLS; val ++)
{
do
{
cout <<"\nEnter score number "<<num +1<<" in percent out of 100 for "<< classArray[val]<<" : ";
cin >> tableGrade[num][val];
while (tableGrade[num][val] < 0 || tableGrade[num][val] > 100)
{
cout<<"The value must be between 0 and 100: ";
cin >> tableGrade[num][val];
cout<<"\n";
}
if (cin.fail())
{
cin.clear();
cin.ignore(INT_MAX, '\n');
}
}while (cin.fail());
}
for (int val = 0, num = 2; val < COLS; val ++)
{
do
{
cout <<"\nEnter score number "<<num +1<<" in percent out of 100 for "<< classArray[val]<<" : ";
cin >> tableGrade[num][val];
while (tableGrade[num][val] < 0 || tableGrade[num][val] > 100)
{
cout<<"The value must be between 0 and 100: ";
cin >> tableGrade[num][val];
cout<<"\n";
}
if (cin.fail())
{
cin.clear();
cin.ignore(INT_MAX, '\n');
}
}while (cin.fail());
}
for (int val = 0, num = 3; val < COLS; val ++)
{
do
{
cout <<"\nEnter score number "<<num +1<<" in percent out of 100 for "<< classArray[val]<<" : ";
cin >> tableGrade[num][val];
while (tableGrade[num][val] < 0 || tableGrade[num][val] > 100)
{
cout<<"The value must be between 0 and 100: ";
cin >> tableGrade[num][val];
cout<<"\n";
}
if (cin.fail())
{
cin.clear();
cin.ignore(INT_MAX, '\n');
}
}while (cin.fail());
}
for (int val = 0, num = ROWS; val < COLS; val ++)
{
do
{
cout <<"\nEnter score number "<<num +1<<" in percent out of 100 for "<< classArray[val]<<" : ";
cin >> tableGrade[num][val];
while (tableGrade[num][val] < 0 || tableGrade[num][val] > 100)
{
cout<<"The value must be between 0 and 100: ";
cin >> tableGrade[num][val];
cout<<"\n";
}
if (cin.fail())
{
cin.clear();
cin.ignore(INT_MAX, '\n');
}
}while (cin.fail());
}
showMenu(CLASS1, CLASS2, CLASS3, CLASS4, CLASS5, CLASS_ALL, choice);
while (choice < 1 || choice > 7)
{
cout<"Please choose an option from 1 to 6: ";
cin>>choice;
if (choice==7)
{
system("PAUSE");
exit;
}
}
//large switch statement where I decided to send the out of the 2d arrays
switch (choice)
{
case 1:
cout <<"Your Grades for "<< classArray[0] << " are.\n";
for (int num =0; num < COLS; num++ )
{
cout<<"Grade: "<<num +1<< tableGrade[num][0]<<"\n";
}
break;
case 2:
cout <<"Your Grades for "<< classArray[1] << " are.\n";
for (int num =0; num < COLS; num++ )
{
cout<<"Grade: %"<<num +1<< tableGrade[num][1]<<"\n";
}
break;
case 3:
cout <<"Your Grades for "<< classArray[2] << " are.\n";
for (int num =0; num < COLS; num++ )
{
cout<<"Grade: %"<<num +1<< tableGrade[num][2]<<"\n";
}
break;
case 4:
cout <<"Your Grades for "<< classArray[3] << " are.\n";
for (int num =0; num < COLS; num++ )
{
cout<<"Grade: "<<num +1<< tableGrade[num][3]<<"\n";
}
case 5:
cout <<"Your Grades for "<< classArray[4] << " are.\n";
for (int num =0; num < COLS; num++ )
{
cout<<"Grade: "<<num +1<< tableGrade[num][4]<<"\n";
}
//side ways for loops cool. to dump my 2d into a one dimension arrays than send them to the sort function
case 6:
cout<<"The sorted grades for your classes are.\n";
for(int var = 0, big =0; var < COLS; var ++, big++){newGrade[big] = tableGrade[0][var]; }
for(int var = 0, big =5; var < COLS; var ++, big++){newGrade[big] = tableGrade[1][var]; }
for(int var = 0, big =10; var < COLS; var ++, big++){newGrade[big] = tableGrade[2][var]; }
for(int var = 0, big = 15; var < COLS; var ++, big++){newGrade[big] = tableGrade[3][var]; }
for(int var = 0, big = 20; var < COLS; var ++, big++){newGrade[big] = tableGrade[4][var]; }
//sort the large grade table
goSort(newGrade, NEW_GRADE);
break;
default:
cout<<"Error invalid input entered"<<endl;
break;
}
cout<<"Do you wish to exit the program now\n";
cout<<
system("PAUSE");
exit;
}
//while loop decrementing with some symbols
void goSplash()
{
int count = 1000;
while (count > 0)
{
count--;
cout<<"101101111100000001101010010100100100111110010101001010101110101";
if (count%5==0)
{
cout<<"%^%&^(^&(^*%^%*%%^$(^&%^$*$*$%*$%*%**%*";
}
}
cout<<setw(50)<<"CHRIS'S GRADE KEEPER";
system("CLS");
}
void showMenu(const int CLASS1, const int CLASS2, const int CLASS3, const int CLASS4, const int CLASS5, const int CLASS_ALL, int &choice)
{
const int EXIT = 7;
cout<<"*****MENU*****\n";
string menuStar = "*****";
for (int menu=50; menu > 5; menu -=5 )
{
cout<<menuStar<<endl;
}
cout<<"\n";
cout<<CLASS1<<"-- to display you first class scores\n";
cout<<CLASS2<<"-- to display you second class scores\n";
cout<<CLASS3<<"-- to display you third class scores\n";
cout<<CLASS4<<"-- to display you fourth class scores\n";
cout<<CLASS5<<"-- to display you fifth class scores\n";
cout<<CLASS_ALL<<"--to display all of your class scores sorted\n";
cout<<EXIT<<"--to exit the program\n\n";
cout<"Please choose an option from 1 to 7: ";
do{
cin>>choice;
if (choice == 7)
{
system("pause");
exit (0);
}
}while (choice ==7);
}
void goSort(int newGrade[], const int NEW_GRADE )
{
for (int k = 1; k < NEW_GRADE; k++)
for (int i = 0; i <new_grade -1="" -="" mode="hold" /> if (newGrade[i] > newGrade[i +1]){
int temp = newGrade[i];
newGrade[i] = newGrade[i + 1];
newGrade[i + 1] = temp;
}
for (int count = 0; count < NEW_GRADE; count++)
{
cout<<"Grade "<<count<<" :"<<newGrade[count]<<"\n";
}
}
解决方案
(^&%^
*
*
这篇关于无法从字符串数组中获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文