Idk whats wrong here?

So i just sent in this code for school so its all said and done but i don't understand what i changed with the msgMulti function where it won't work anymore. So it displays the prompt asking the person for the message, but it doesn't take an input for the message. it skips that and goes to the input for the number of times the message is repeated. On the other hand, when i pull it by its self as its own program, it works just fine.

msgMulti is the fish function down, and int main is the last

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>//input output
#include <cstring>//strings
#include <cmath>//use of pow and sqrt
#include <iomanip>

using namespace std;//calls functions with returns
char menuApplication(char);
string welcomeMsg(string);
int numSize();


void personalHeader()
{
    cout<<"Name: Will\nMajor: Electrical Engineering\nFunction Testing Program:)"<<endl;//header
    cout<<"\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\n"<<endl;
}

string welcomeMsg(string userName)//string type function taking a name and returning it to main for a display message
{
    cout<<"What's your name? Type in and press ENTER\nName: ";
    getline(cin, userName);
    return userName;//returns it to int for only the word not the whole function
}

void dayMenu()
{
    char selection;
    cout<<"1 - Monday\n2 - Tuesday\n"<<"3 - Wednsday\n4 - Thursday\n5 - Friday\n6 - Saturday\n7 - Sunday\nPlease input a number that corresponds with the day you wish to select and press ENTER\nSelection: ";
    cin>>selection;
    if(selection=='1'||selection=='2'||selection=='3'||selection=='4'||selection=='5'||selection=='6'||selection=='7')
    {//if the selection is the char 1 through 7 then menuApp function is called, using || was doing weird things on xcode?
        menuApplication(selection);//calls the menu
    }
    else//otherwise it recalls its own function with a message before it that say you need to enter number correctly
    {
        cout<<"Please use a number between 1 and 7 and press ENTER"<<endl;
        dayMenu();//the recall
    }
}

char menuApplication(char daySelection)//function that displays what day it is
{
    switch(daySelection)//switch statement to make a simple menu with input from the upper function of 1 through 7
    {
        case '1' : cout<<"So today is Monday!\n";
            break;
        case '2' : cout<<"So today is Tuesday!\n";
            break;
        case '3' : cout<<"So today is Wednsday!\n";
            break;
        case '4' : cout<<"So today is Thursday!\n";
            break;
        case '5' : cout<<"So today is Friday!\n";
            break;
        case '6' : cout<<"So today is Saturday!\n";
            break;
        case '7' : cout<<"So today is Sunday!\n";
            break;
    }
    cout<<"\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\n"<<endl;
    return 0;
}


void multiMsg()// reads a line and displays it an input amount of times
{
        string msgToBeMulti;
    int count, i;// int count for number of displays and i for the for loop
    cout<<"What message would you like to be read?"<<endl<<"Message: ";
    getline(cin, msgToBeMulti);  //gets message
    cout<<endl;//gets message
    cout<<"How many times would you like it to be read?\nMultiplied By: ";
    cin>>count;//times
    for(i=0; i <= count ; i++)//for loop that stops displaying when i is incremented by 1 untill it is equal to count
    {
        cout<<msgToBeMulti<<endl;
    }
}

int numSize()//compaires 2 numbers
{
    int num1, num2;
    cout<<"This compares 2 numbers and ouputs the larger number.\nWhats number 1?\nNumber 1: ";
    cin>>num1;//num 1
    cout<<"Whats number 1?\nNumber 2: ";
    cin>>num2;//num2
    if(num1>num2)// an if loop to compair num1 to num2
    {
        cout<<"Of Number 1: "<<num1<<" and Number 2: "<<num2<<"\n Number 1: "<<num1<<" is larger."<<endl;
        //if the staement is true it will output this message
        cout<<"\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\n"<<endl;
    }
    else//otherwise it compaires num2 to num1
        if(num2>num1)
        {
            cout<<"Of Number 1: "<<num1<<" and Number 2: "<<num2<<"\n Number 2: "<<num2<<" is larger."<<endl;
            //if true it will output this message
            cout<<"\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\n"<<endl;
        }
        else//if both are false then it will output this message and call its own function again
        {cout<<"Both numbers are the same, please enter another set of numbers."<<endl;
            numSize();
        }
    return 0;
}

void cartesianPoints()
{
    int xcord1, ycord1, xcord2, ycord2, xsquare, ysquare;
    double cordDist;
    cout<<"Enter each Cartesian point in the form of X_Y, with a space between each point and the press ENTER.\nEx. 1 2 3 4[ENTER}\nPOINTS: ";
    cin>>xcord1>>ycord1>>xcord2>>ycord2;
    xsquare = pow((xcord2 - xcord1),2);//squares the difference
    ysquare = pow((ycord2 - ycord1),2);
    cordDist = sqrt(xsquare + ysquare);//sqroots the sum
    cout<<"The distance between the two points ( "<<xcord1<<" , "<<ycord1<<" ) and ( "<<xcord2<<" , "<<ycord2<<" ) is\n";
    cout<<"Distance: "<<cordDist;
    cout<<"\n\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\n\n"<<endl;
    
}


int main()
{
    string returnName;//variable returnname
    personalHeader();
    returnName =  welcomeMsg(returnName);//calls function and grabs the input of that function and addresses it to return name
    cout<<"Welcome "<<returnName<<"!"<<endl;
    cout<<"\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\n"<<endl;
    dayMenu();//calls all functions
    multiMsg();
    numSize();
    cartesianPoints();
    return 0;
    
}
Last edited on
You have the same problem; See my post below.
http://www.cplusplus.com/forum/beginner/201753/#msg961253
I think (but not quite sure) that the problem is that you are using a mixture of straight cin>> to get data and getline(cin, ...). The first of these will extract the requisite number of data items from the stream but it doesn't need to complete the line, so it doesn't go past the newline character ... the next call to getline will take all remaining data up to the newline (\n) character - which in this case won't be much if any. It can then go on.

You can fix your immediate problem by finishing the line in dayMenu as below (but it will probably occur in other places as well: I haven't checked.)

1
2
3
4
5
6
void dayMenu()
{
    char selection;
    cout<<"1 - Monday\n2 - Tuesday\n"<<"3 - Wednsday\n4 - Thursday\n5 - Friday\n6 - Saturday\n7 - Sunday\nPlease input a number that corresponds with the day you wish to select and press ENTER\nSelection: ";
    cin>>selection;
    cin.ignore( 1000, '\n' );    // complete the line here 


I don't advocate mixing cin>> and getline( cin, ..). A safe alternative is to use getline for everything and stringstream the resulting string into whatever data is required.
Topic archived. No new replies allowed.