Thread: Array [Wayy less complicated than other posts]

  1. #1
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Array [Wayy less complicated than other posts]

    ok, well, looking at the other posts in this forum I'm the least experienced here, hope you all don't mind me asking these questions.

    Now, I've been learning about arrays the past few days and have no problems getting text to display from one, but, how do you take text from a 'char' array by inputting a number?
    Here is what I'm trying to do:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    	const int months[]={'j','f','m','a','m','j','j','a','s','o','n','d'};
    	const int days[]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	int kbi=0, int kbin=0, int count=0;
    	cout << "Enter date in # format (ie, 1,2,3...12): ";
    	cin >> kbi;
    	kbin=(kbi-1);
    
    	for(;kbin=kbin	
    {	
    	cout << "The month " << months[kbin] << " has " 
    		 << days[kbin] << " days in it.";
    
    system("pause");
    return 0;
    }
    //kill
    system("pause");
    return 0;
    }
    For some reason when I enter 1 for 'j' January I get "The month 106 has 31 days in it", umm, that's a far cry from the 31 it's supposed to have.
    I heard of a static_cast a few days back, is that needed here?

    Thanks for your help (and previous help)

    Rob
    [Note: I'm not comparing c++ to batch this time lol]

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    when you assign a 'char' to an integer.. you are assigning the ascii value of that char into the integer variable..

    you can do that if you want.. but if you want to display the character instead of the ascii value.. you will have to cast your integer into a char:

    Code:
    
    char calandar_months[12]; 
    
    
    for(int i=0; i<12; i++)
    
         calandar_months[i] = static_cast<char>(months[i]);
    
    //calandar_months[]  now contains 'letters' instead of numeric ascii values
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    You are outputting the integer equivalent of the ascii value of 'j' - type cast the month[kbin] variable to a char via:

    Code:
    cout << "The month " << (char)months[kbin] << " has "
    This will print out the correct letter for you.

    Hehe Thebrain beat me to it

  4. #4
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Thanks again, should just hire you 2 as tutors!

    Thanks again, I was suspecting it was going to be static_cast but wasn't sure how to use it.

    Well, lol, maybe I'll be able to help you back someday,


    have a good one,

    (I'll hopefully have one of those 'complicated' questions next time! lol, that'll mean I've actually beat the super-novice stage!!!)

    Rob Sitter

  5. #5
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    To end this thread >>Found what I was looking for

    Thanks ventolin & thebrain!
    But, as it turns out I was looking for a multi-dimensional array to get the result I wanted.
    Thanks anyhow! Appreciate it!
    Now I know what static_cast does anyhow

    Code:
    // 01.cpp : Defines the entry point for the console application.
    // Stupid calendar that won't work
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    	const char months[12][20]=
    	{	"January",
    		"February",
    		"March",
    		"April",
    		"May",
    		"June",
    		"July",
    		"August",
    		"September",
    		"October",
    		"November",
    		"December"
    	};
    	int kb=0, kbi=0;
    	cout << "Enter date in # format (ie, 1,2,3...12): ";
    	cin >> kb;
    	kbi=(kb-1);
    
    	while(kbi > 0)	
    {	
    	cout << "(" << months[kbi] << ") has " << " days in it.\n";
    	system("pause");
    	return 0;
    }
    //kill
    system("pause");
    return 0;
    }
    Last edited by Robert_Sitter; 10-30-2005 at 09:16 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    If you are casting something, then you should rethink your code. A beginner should avoid doing any casting.
    Last edited by 7stud; 10-30-2005 at 09:30 PM.

  7. #7
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Very Last Post on this thread ( I swear !!!)

    There, I've figured it out, no casting or anything. Had put a double array where it didn't belong (days) and wasn't calling them properly.
    Damn this can be COMPLICATED lol, wonder if anyone else thought that a week in...

    Until next time people,

    Rob Sitter

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {
    	const char months[12][10]=
    	{	"January","February","March","April","May","June","July",
    		"August","September","October","November","December"
    	};
        const double days[12]=
    	{	31,29,31,30,31,30,31,31,30,31,30,31
    	};
    
    
    	int kb=0, kbi=0;
    	cout << "Enter date in # format (ie, 1,2,3...12): ";
    	cin >> kb;
    	kbi=kb-1;
    
    	while(kbi > 0)	
    {	
    	cout << months[kbi] << " has " << days[kbi] << " days in it.\n";
    	system("pause");
    	return 0;
    }
    //kill
    system("pause");
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM