Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,564 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,895 people online right now. Registration is fast and FREE... Join Now!




pass integer to the function and display string

2 Pages V  1 2 >  
Reply to this topicStart new topic

pass integer to the function and display string

KMH
8 Apr, 2008 - 08:39 PM
Post #1

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Guys I got the code working ok but one problem I have is that it didn't display what I want it to display.

CODE


This is function prtotype I put it in the header file:

char *n_chars(char, int);

This is the main.cpp file:

#include <iostream>
#include <string>
#include "GreenFunction.h"

using namespace std;
char* get_num_month(int month);

int main()

{
    int mm=0;
    char ch = 0;
    char *month;
    while(ch != 'q') // to quit
    {
    cout << "Enter month number I will display it in string: ";
    cin >> mm;
    cout << "Enter another number or press the q-key to quit: ";
    cin >> ch;
    
    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
        {
         cout << "INVALID month: Please try again." << endl;
         cin.get();
        }
    else
        {
        month = get_num_month(mm);
        
        cout << "The month you entered: " << mm << " is :" << month << endl;
        }
}
//cin.get();
return 0;    
}


And this is my FunctinCall.cpp file:

#include <iostream>

#include "GreenFunction.h"
char* get_num_month(int month)
{
    using namespace std;
    static char arr[][10] = {"January","february","March","April","May","June","July","August","September","October","November","December"};
    return arr[month-1];
            
}  

When I run it this what I got:

Enter month number I will display it in string: 3
Enter another number or press the q-key to quit: 2
The month you entered: 3 is: March
Enter month number I will display it in string:



All I want it do is this:

Enter month number I will display it in string: 3
The month you entered: 3 is: March
Enter another number or press the q-key to quit: q

I can not see what I did wrong, can anyone help me see what I did wrong, please help.

Want to thank you in advance for all your help

Regards,

KMH


User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Pass Integer To The Function And Display String
8 Apr, 2008 - 08:43 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
If I understand your question correctly...

cpp

int main(void) {
int mm=0;
char ch = 0;
char *month;
while(ch != 'q') { // q to quit
cout << "Enter month number I will display it in string: ";
cin >> mm;

if( mm < 1 || mm > 12 ) { // Validate to see if input for month is valid
cout << "INVALID month: Please try again." << endl;
cin.get();
}
else {
month = get_num_month(mm);
cout << "The month you entered: " << mm << " is :" << month << endl;
}
cout << "Enter another number or press the q-key to quit: ";
cin >> ch;
}
return 0;
}

User is offlineProfile CardPM
+Quote Post

KMH
RE: Pass Integer To The Function And Display String
8 Apr, 2008 - 09:01 PM
Post #3

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Hi no2pencil,

Thank you very much for your reply, this is what I got after I make changes:

Enter month number I will display it in string: 3
The month you entered: 3 is: March
Enter another number or press the q-key to quit: 2
Enter month number I will display it in string:

Still did not work correctly....
User is offlineProfile CardPM
+Quote Post

KMH
RE: Pass Integer To The Function And Display String
8 Apr, 2008 - 09:23 PM
Post #4

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Guys I got it working, I move the line cout << "Enter month number I will display it in string : " <<; put it above pass the while loop, that way it will not got call everytime, see the code below how I did it.

CODE


#include <iostream>
#include <string>
#include "GreenFunction.h"

using namespace std;
char* get_num_month(int month);

int main()

{
    int mm = 0;
    char ch = 0;
    char *month;

    cout << "Enter month number I will display it in string: ";
    cin >> mm;

    while(ch != 'q') // to quit
    {
            
    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
        {
         cout << "INVALID month: Please try again." << endl;
         cin.get();
        }
    else
        {
        month = get_num_month(mm);
        
        cout << "The month you entered: " << mm << " is :" << month << endl;
        }
        cout << "Enter another number or press the q-key to quit: ";  
        cin >> ch;  
    }
return 0;    
}





QUOTE(no2pencil @ 8 Apr, 2008 - 09:43 PM) *

If I understand your question correctly...

cpp

int main(void) {
int mm=0;
char ch = 0;
char *month;
while(ch != 'q') { // q to quit
cout << "Enter month number I will display it in string: ";
cin >> mm;

if( mm < 1 || mm > 12 ) { // Validate to see if input for month is valid
cout << "INVALID month: Please try again." << endl;
cin.get();
}
else {
month = get_num_month(mm);
cout << "The month you entered: " << mm << " is :" << month << endl;
}
cout << "Enter another number or press the q-key to quit: ";
cin >> ch;
}
return 0;
}



User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Pass Integer To The Function And Display String
8 Apr, 2008 - 09:26 PM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Awesome... congrats!

*in respect to Martr2* Here at Dream In Code we be tree huggin' ninjas. ph34r.gif
User is offlineProfile CardPM
+Quote Post

KMH
RE: Pass Integer To The Function And Display String
8 Apr, 2008 - 09:51 PM
Post #6

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

I spoke to soon, it still di not work right, the quit part work ok if I press q-key it quit, but if I enter another number I got:

Enter month number I will display it in string: 3
The month you entered: 3 is: March
Enter another number or press the q-key to quit: 2
Enter month number I will display it in string:


QUOTE(no2pencil @ 8 Apr, 2008 - 09:43 PM) *

If I understand your question correctly...

cpp

int main(void) {
int mm=0;
char ch = 0;
char *month;
while(ch != 'q') { // q to quit
cout << "Enter month number I will display it in string: ";
cin >> mm;

if( mm < 1 || mm > 12 ) { // Validate to see if input for month is valid
cout << "INVALID month: Please try again." << endl;
cin.get();
}
else {
month = get_num_month(mm);
cout << "The month you entered: " << mm << " is :" << month << endl;
}
cout << "Enter another number or press the q-key to quit: ";
cin >> ch;
}
return 0;
}



User is offlineProfile CardPM
+Quote Post

KYA
RE: Pass Integer To The Function And Display String
8 Apr, 2008 - 11:00 PM
Post #7

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
Because the any input after the initial one is "ch" while the first is "mm" The selection structure does not account for "ch". I'll post some code in a sec, I'm on my laptop--hard to type.
User is offlineProfile CardPM
+Quote Post

KMH
RE: Pass Integer To The Function And Display String
9 Apr, 2008 - 12:28 AM
Post #8

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Hello KYA,

I have changed the structure of the code, I declared a function with 2 parameters and I never use it. Now I have 2 error: error C2065: 'mm' : undeclared identifier in the main.cpp file (n_chars(arr,mm)wink2.gif and error C2447: '{' : missing function header (old-style formal list?) in the FunctionCall.cpp file I have seen this one before and it is seem that I missing ({}) somewhere in the code, but I can't see it.

CODE


This is Function prototype
void n_chars(char arr, int mm);// Function proto type

This main.cpp file
#include <iostream>
#include <string>
#include "GreenFunction.h"
using namespace std;

int main()
{
    
    char *arr[][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    n_chars(arr,mm);
    cin.get();
    return 0;
}

And this is the functionCall.cpp
#include <iostream>
#include "GreenFunction.h"
using namespace std;
void n_chars(char *arr, int *mm);


{
    int mm;  
    char ch;

    while(ch != 'q')// q to quit
        {    
        cout << "Enter month number I will display it in string: ";  
        cin >> mm;  
          
        if( mm < 1 || mm > 12 )// Validate to see if input for month is valid  
            {
             cout << "INVALID month: Please try again." << endl;  
             cin.get();  
            }  
        else
            {  
            month = get_num_month(mm);  
            cout << "The month you entered: " << mm << " is :" << month << endl;  
            }  
        cout << "Enter another number or press the q-key to quit: ";  
        cin >> ch;  
         }  
  
    return arr[month-1];
}  


QUOTE(KYA @ 9 Apr, 2008 - 12:00 AM) *

Because the any input after the initial one is "ch" while the first is "mm" The selection structure does not account for "ch". I'll post some code in a sec, I'm on my laptop--hard to type.


User is offlineProfile CardPM
+Quote Post

KYA
RE: Pass Integer To The Function And Display String
9 Apr, 2008 - 02:50 AM
Post #9

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
Use your old code and just have it ask if the user wants to input another number. Then have it loop again asking for a new month #. This solves your problem without creating any new ones.


User is offlineProfile CardPM
+Quote Post

KMH
RE: Pass Integer To The Function And Display String
9 Apr, 2008 - 08:53 PM
Post #10

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Hello KYA;

Thank you for your help, I did try what I have below and when user enter " no " it work well, but when user enter "yes" this is what I got, perhapes my loop is in the wrong palce??
CODE

#include <iostream>
#include <string>
#include "GreenFunction.h"

using namespace std;
char* get_num_month(int month);

int main()

{
    int mm = 0;
    char ch = 0;
    char *month;

        cout << "Enter month number I will display it in string: ";
        cin >> mm;
  
    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
        {
         cout << "INVALID month: Please try again." << endl;
         cin.get();
        }
    else
        {
        month = get_num_month(mm);
        
        cout << "The month you entered: " << mm << " is :" << month << endl;
        cout << "Do you wish to enter another number? <y/n> ";
        cin >> ch;
        cin.get();
    if(ch == 'y' || ch == 'Y')
        cout << "The month you entered: " << mm << " is :" << month << endl;
    else if(ch== 'n' || ch == 'N')
        cout << "Good bye and have a wonderfull day!" << endl;
        }
        
cin.get();  
return 0;    
}


Enter month number I will display it in string: 3
The month you entered: 3 is: March
Do you wish to enter another number? <y/n> n
The month you entered: 3 is: March


It's seem to repeated the first one, please help.
User is offlineProfile CardPM
+Quote Post

KMH
RE: Pass Integer To The Function And Display String
9 Apr, 2008 - 09:10 PM
Post #11

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

Hi KYA,

If I have it loop again then I have to do that 12 times, then I have to adds this part 12 times for the total of 12 months:
CODE

month = get_num_month(mm);
        
        cout << "The month you entered: " << mm << " is :" << month << endl;
        cout << "Do you wish to enter another number? <y/n> ";
        cin >> ch;
        cin.get();
    if(ch == 'y' || ch == 'Y')
    {
        cout << "Enter another month number: ";
        cin >> mm;
                                                   month = get_num_month(mm);
        cout << "The month you entered: " << mm << " is :" << month << endl;
        cin.get();
    }

    else if(ch== 'n' || ch == 'N')
        cout << "Good bye and have a wonderfull day!" << endl;


QUOTE(KYA @ 9 Apr, 2008 - 03:50 AM) *

Use your old code and just have it ask if the user wants to input another number. Then have it loop again asking for a new month #. This solves your problem without creating any new ones.


This post has been edited by KMH: 9 Apr, 2008 - 09:23 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Pass Integer To The Function And Display String
10 Apr, 2008 - 01:05 AM
Post #12

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
You'll need a while or do while loop for the actual program. Insert one and see if it gives you the correct output/prompts
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/2/08 11:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month