在这种情况下如何使用循环? [英] How do I use loop in this case ?

查看:102
本文介绍了在这种情况下如何使用循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++的bignner,我面临一些循环问题,我们被要求写一个

计划,允许学生报名参加课程。该计划还应在每次报名后更新课程信息,并向学生发放课程表。学生最多可以注册两门课程。

如果学生输入字母f或注册两门课程后,课程结束。

假设打开了三门课程及其信息。我尝试了所有循环类型,但它没有用。



我尝试过:



I'm a bignner in c++ and i'm facing some issues with looping and we have been asked to write a
program that allows students to enroll in courses. The program should also update courses information after
each enrollment, and issue the course table to the student. The student can enroll in maximum two courses.
The program ends if the student entered the letter f or after enrolling in two courses.
Assume that three courses are opened and their information. I tried all the looping types and it didn't work.

What I have tried:

While(i<3) {
	Infile>>courseID>>course_name>>max_number>>numofenrol;
	If(course_ID==user_courseID)&&(course_name==user_cname)&&(max_number>numofenrol)
		Cout<<"you are enrolled in "<<course_name<<endl;
	If (course_ID==user_course)&&(course_name==user_name)&&(max_number==numofenrol)
		cout<<"course is full"<<endl;
	i++
}

推荐答案

请看我对这个问题的评论。



修复代码中的大小写。确保在使用前声明并初始化所有内容。不要硬编码任何东西,甚至是3。使它成为一个函数参数。请注意,您为断开循环,f和2个课程制定了两个条件,但您只使用了一个。



的条件下,没有任何东西强迫你写下所有条件语句。您还可以在下编写一些条件,如果代码: if(something)break; 。例如,您可以计算中为循环添加的课程,主要用于计算内容,并在循环体中添加f条件。



对不起,没有代码。这项任务更为微不足道。但更重要的是,我不想抓住机会通过这项任务学到一些东西;只有你自己才能学到它。



-SA
Please see my comment to the question.

Fix casing in code. Make sure everything is declared and initialized before use. Don't hard-code anything, even "3". Make it a function parameter. Note that you formulated two conditions for breaking out of loop, "f" and 2 courses, but you use only one.

Nothing force you to write all condition in the condition of the while or for statements. You can also write some condition under if in code: if (something) break;. For example, you could count added courses in for loop, which is primarily designed for counting things, and add condition for "f" in the loop body.

Sorry, no code. The assignment is more that trivial. But more importantly, I don't want to take out your chance to learn something through this assignment; and you only can learn if you do it by yourself.

—SA


这篇关于在这种情况下如何使用循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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