C ++ String Take Probelm [英] C++ String Taking Probelm

查看:79
本文介绍了C ++ String Take Probelm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我用C ++制作Littler程序使用Class,但是我仍然遇到一个错误,当我从用户那里拿学生编号然后它跳过一个名字,从第二个名字开始,请告诉我关于这个问题。

Hello Everyone!

I Am Making Littler program in C++ Using Class, But i STILL got one Error,When i take Student Number From the User then it skip the One Name And Start From the 2nd Name ,Please Tell Me About the Problem.

// student_Record_Management_C++_1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class Menu_Account
{
	private:
		char name[3][20];
		int i;
public:
	void get_name(int);
};
//Get Name From The User
void Menu_Account::get_name(int student)
	{
		cout<<"\n\t\t\tEnter Student Name."<<endl;
		i=0;
		do
		{//Problem Start From There
			cout<<"Enter Student "<<i+1<<" Name:";
			cin.getline(name[i],20);
		}while(i<student);>
		cout<<"Enter Name is."<<endl;
		for ( i = 0; i < student; i++)
		{
			cout<<name[i]<<endl;
		}
	}
int _tmain(int argc, _TCHAR* argv[])
{
	int student=0;
	Menu_Account obj;
	cout<<"How Many Student You Have:";
	cin>>student;
	obj.get_name(student);
	system("pause");
	return 0;
}

推荐答案

问题是你永远不会增加 i i do ... while 循环中总是相同的,所以循环将是无限的。在 do ... while 循环的代码末尾,添加 i ++;



此外,带有学生姓名的阵列大小为3.如果给定学生人数超过3,您的课程将无法存储超过3名学生数组。所以你需要检查给定数量的学生是否大于3.而不是硬编码那个数字,你可以写一个 #define 语句给它使它成为一个常数(你可以做同样的长度)。为什么?因为如果你在多个地方使用相同的数字,你只需要更改常数的值,而不是所有出现的数字。



和Wes Aday一样说,你需要使用 cin.ignore()来忽略终止的换行符,它仍在流上。



因此,您可以将代码更改为:



Menu_Account class:

The problem is that you never increment the value of i. i will always be the same in your do ... while loop, so the loop will be infinite. At the end of the code in your do ... while loop, add i++;.

Also, the size of your array with student names is 3. And if the given number of students is more than 3, your program won't be able to store more than 3 students in the array. So you'll need to check whether the given number of students is greater than 3. And instead of hard-coding that number, you can write a #define statement for it to make it a constant (you can do the same for the length). Why? Because if you use the same number at multiple places, you just have to change the value of your constant, instead of all occurrences of the number.

And as Wes Aday said, you'll need to use cin.ignore() to ignore the terminating newline character, which is still on the stream.

So, you can change your code into this:

Add this line of code before your Menu_Account class:
#define MAX_STUDENTS 3
#define MAX_LENGTH 20



然后,在 Menu_Account 类中,替换 char name [3] [20]; with:


Then, in your Menu_Account class, replace char name[3][20]; with:

char name[MAX_STUDENTS][MAX_LENGTH];



然后,更新 get_name 方法:


And then, update your get_name method:

void Menu_Account::get_name(int student)
{
    if (student > MAX_STUDENTS)
    {
        cout<<"Number too high, there are max. "<<MAX_STUDENTS<<" accepted."<<endl;
        return;
    }
    cout<<"\n\t\t\tEnter Student Name."<<endl;
    i=0;
    do
    {//Problem Start From There
        cout<<"Enter Student "<<i+1<<" Name:";
        cin.getline(name[i],20);
        cin.ignore();
        i++;
    }while(i<student);
    cout<<"Enter Name is."<<endl;
    for ( i = 0; i < student; i++)
    {
        cout<<name[i]<<endl;
    }
}


您好,



在您的代码中人们告诉,输入学生号码后,你按下输入按钮。因此,这将仍然在缓冲区中,并将作为第一个学生的名称。为了避免这种行为,在 cin.getline(name [i],20)之后; 行位置 cin.Ignore()或 getchar() 。然后你会得到你想要的结果。
Hi,

In your code as people told, after entering students number , you are pressing enter button. So this will be still in buffer and which will be taken as name for first student. To avoid this behavior, after cin.getline(name[i],20); line place cin.Ignore() or getchar(). Then you will get your desired result.


随时欢迎你,最后你得到了解决方案。很高兴听到你。
You are always welcome, finally you got the solution. Its pleasure to hear you.


这篇关于C ++ String Take Probelm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆