char arrays, pointers, etc, could use help badly

This is a discussion on char arrays, pointers, etc, could use help badly within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> using namespace std; void displayDay(char *ptr); void displayDayAddress(char *ptr); int main(){ char *days[] = {"Sunday", "Monday", "Tuesday", ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    char arrays, pointers, etc, could use help badly

    Code:
    #include <iostream.h>
    using namespace std;
    
    void displayDay(char *ptr);
    void displayDayAddress(char *ptr);
    
    int main(){
    	char *days[] = {"Sunday",
    					"Monday",
    					"Tuesday",
    					"Wednesday",
    					"Thursday",
    					"Friday",
    					"Saturday"};     //array of char pointers
    	int selectedDay;
    	char *ptr;       //character pointer
    	
    	cout << "OUTPUT FOR PROBLEM #6a and #6b: " << endl;
    	cout << "What day is it today? (1 - 7): ";
    	cin >> selectedDay;
    	ptr = days[selectedDay - 1];        //store address of the given array element in ptr
    	displayDay(ptr);
    	
    	cout << "\n\nOUTPUT FOR PROBLEM #6c: " << endl;
    	cout << "This day's address begins at: ";
    	displayDayAddress(ptr);
    	
        return 0;
    } //end int main
    
    
    void displayDay(char *ptr){
    	cout << "Today's day is " << ptr;
    	 
    } //end void displayDay
    
    
    void displayDayAddress(char *ptr){
    	cout << &ptr;
    }
    For some reason I can't do what I want to do. What I want to do:

    -Send into a function that large *char array of months, and an int
    -In that function, the user will input a number into this int
    -That day is called from the function. For instance, entering 2 will display Monday

    -Then I need to make another function that outputs the address of the first character of the day inputted.


    However, I can't get this to work right! I can't pass the values correctly so the majority of it is in main!! I could really use some assistance
    Last edited by Nakeerb; 10-11-2002 at 09:47 PM.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Whenever I said months I meant days

  3. #3
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    i'm a terrible person, as i am too lazy to run this through a c++ compiler. how about some error messages/bad output?
    .sect signature

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    It works but it's poorly made. The second part where I want to output the address does NOT work.

    I just want to put the bulk of this code in their respective functions. As in, I just want to declare my variables in main, and have all my input and output and such in the functions, and have that second function output the address of the day specified

  5. #5
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    void displayDayAddress(char *ptr){
    cout << ptr;
    }

    that should work (i think, maybe?)

    &ptr is the memory location of the pointer
    ptr is the value of the pointer (the address of the item)
    *ptr is the value of the address the pointer points to

    buggered pointer rules...

    i don't really see why you want to break your program down into smaller functions, unless you're just appeasing a professor ;p - it doesn't look so bad as it is. in fact, i wouldn't even bother to make any functions for that.

    if you've gotta, then just write the functions out... and pass the address of the variables that will store the input to the function as pointers.
    .sect signature

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    I am still at a loss though, I need to put the bulk of this into the functions but I can't pass them correctly.

    And I also need to correctly output the address

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Works fine to me. But I put it into a loop, and wrote a function "range" to verify the user didn't type '0', or whatever (guess what "days[-1]" would print??)


    Code:
    using namespace std;
    
    bool range(int value, int low, int high);
    void displayDay(char *ptr);
    void displayDayAddress(char *ptr);
    
    int main(){
    	char *days[] = {"Sunday",
    					"Monday",
    					"Tuesday",
    					"Wednesday",
    					"Thursday",
    					"Friday",
    					"Saturday"};     //array of char pointers
    	int selectedDay;
    	char *ptr;       //character pointer
    
    	cout << "OUTPUT FOR PROBLEM #6a and #6b: " << endl;
    	cout << "What day is it today? (1 - 7): ";
     	cin >> selectedDay;
    
      while(range(selectedDay, 1, 7)) {
    	 ptr = days[selectedDay - 1];        //store address of the given array element in ptr
    	 displayDay(ptr);
    	 cout << "\n\nOUTPUT FOR PROBLEM #6c: " << endl;
    	 cout << "This day's address begins at: ";
    	 displayDayAddress(ptr);
         cout << "OUTPUT FOR PROBLEM #6a and #6b: " << endl;
    	 cout << "What day is it today? (1 - 7): ";
      	 cin >> selectedDay;
         }
        return 0;
    } //end int main
    
    
    void displayDay(char *ptr){
    	cout << "Today's day is " << ptr << endl;
    	 
    } //end void displayDay
    
    
    void displayDayAddress(char *ptr){
    	cout << &ptr << endl;
    }
    
    bool range(int value, int low, int high){
     if(value >= low && value <= high)
      return true;
    
     return false;
    }
    Last edited by Sebastiani; 10-12-2002 at 05:45 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    The address thing does not work and the bulk of the code needs to be in the functions, though. How can I just pass the values correctly into another function?

    And the address thing only displays one address no matter what I do. I need it so, like, if I enter in 2 for Monday it gives me the address of the "M" in Monday, and likewise with 7 for Saturday, it outputs the memory location of that "S"

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ah, noticed the address bug...

    void displayDayAddress(char *ptr){
    cout << (int)ptr << endl;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21