gets(name) with loops

Feb 26, 2015 at 11:17pm
So I am using gets(name) in a loop. When I get through the fist loop the second loop skips over the gets(name) statement. Do I need to clear the gets(name) some how? This is made as a separate function from my main. The main loops through a series of functions but on the second pass it skips this function and goes to the next.

1
2
3
4
5
6
7
8
9
char readName(char name[])

{

    printf("\nName:");
    gets(name);

    return name;
}
Feb 26, 2015 at 11:42pm
Please post more of the code so we can help figure out the problem.

Also, please describe what the purpose of the code is.
Feb 26, 2015 at 11:57pm
ok here is my main. The purpose of this program is to accept someone's assignment scores quiz scores and exam scores and calculate there grade based upon that info. My trouble is with getting the name. On the first pass it asks for the name of the student. I enter it in, and then it displays the name later. Once I get through the whole function it ask if I would like to go again. So when I enter yes it displays Name: and goes right to the first question. So on the second pass it skips get(name)

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
int main()
{
    char name[MAX];
    int assigns[ASSIGN_TOTAL], quizzes[QUIZ_TOTAL], exams[EXAMS_TOTAL], finalExam;
    double assignsMedian, assignsMean, assignsPerc;
    double quizzesMedian, quizzesMean, quizzesPerc;
    double examsMedian, examsMean, examsPerc, finalPerc, classPerc, classGrade;

    do
    {
        readName(name);

        fillArray(assigns, ASSIGN_TOTAL, "assignments");
        selectionSort(assigns, ASSIGN_TOTAL);
        assignsMedian = findMedian(assigns, ASSIGN_TOTAL);
        assignsMean = findMean(assigns, ASSIGN_TOTAL);
        assignsPerc = calcPercentage(assigns, ASSIGN_TOTAL, ASSIGN_MAX);


        fillArray(quizzes, QUIZ_TOTAL, "quizzes");
        selectionSort(quizzes, QUIZ_TOTAL);
        quizzesMedian = findMedian(quizzes, QUIZ_TOTAL);
        quizzesMean = findMean(quizzes, QUIZ_TOTAL);
        quizzesPerc = calcPercentage(quizzes, QUIZ_TOTAL, QUIZ_MAX);


        fillArray(exams, EXAMS_TOTAL, "exams");
        selectionSort(exams, EXAMS_TOTAL);
        examsMedian = findMedian(exams, EXAMS_TOTAL);
        examsMean = findMean(exams, EXAMS_TOTAL);
        examsPerc = calcPercentage(exams, EXAMS_TOTAL, EXAMS_MAX);


        finalExam = readFinalScore();
        finalPerc = calcfinalPercentage(finalExam, FINAL_MAX);

        classPerc = calcClassPerc(assignsPerc, quizzesPerc, examsPerc, finalPerc);
        classGrade = calcGrade(assignsPerc, quizzesPerc, examsPerc, finalPerc);

        displayResults(name, classPerc, classGrade,
                       assignsPerc, quizzesPerc, examsPerc, finalPerc,
                       assignsMean, quizzesMean, examsMean,
                       assignsMedian, quizzesMedian, examsMedian);

    }while(goAgain());

    return 0;
}
Feb 27, 2015 at 9:04pm
Post the readName(); function you have listed on line 11. The problem is 90% with that function.

What I am assuming is going on without seeing the rest of the code is that you are using

getline after cin << value.

This will not work as the getline will pick up the newline \n in the buffer between the two. Make sure you clear the buffer before you use getline.
Feb 27, 2015 at 9:06pm
gets has been deprecated and is no longer in the latest C standard. It's dangerous. Don't use it.
Topic archived. No new replies allowed.