List of days code help

Hello, new here, I just started c++ last week so go easy..

I'm trying to write a code to display the days of the week like this

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday


I wrote this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main ()
{
	char * dayname[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	;int i;
	for (i = 0; i<7; ++i)
	{
		cout << * dayname[i] << endl;
	}

  return 0;

}


The problem is it gives me back this;

S
M
T
W
T
F
S

Why is it only giving the first letter of each day?

Thanks for any help!
http://www.cplusplus.com/doc/tutorial/ntcs/
http://www.cplusplus.com/doc/tutorial/pointers/

It should be cout << dayname[i] << endl;

What you wrote is essentially this: cout << dayname[i][0] << endl;
Another way of writing it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

int main ()
{
	string dayname[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	
	unsigned short i;
	
	for (i = 0; i<7; i++)
	{
		cout << dayname[i] << endl;
	}

	getch();
        return 0;

}


Last edited on
Branflakes91093
Thank you for your reply. I tried what you said but it gives the same as before..

chaoskie
Your code does work however I don't understand the conio.h or the getch ();

My compiler also gives this message with your code:
Warning 1 warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.


Could anyone tell me how to alter my original code to give the days fully?
We have been learning about "if" statements so if that's relevant I can try that too.
@tcdnewb

Mainly, all that was needed, is to drop the endl, or new line, and use a space or spaces.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Week Days.cpp : main project file.

#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

int main ()
{
	string dayname[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	
	unsigned short i;
	
	for (i = 0; i<7; i++)
	{
		cout << dayname[i]<< "     "; // Used 5 spaces to get days of week to stretch across console
	}

	_getch(); // Located in the Conio.h header file
        return 0;
}
@whitenite1

Thank you for your reply. I understand what you mean about the spaces now, I have this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main ()
{
	char * dayname[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
	int i;
	for (i = 0; i<7; ++i)
	{
		cout << dayname[i] << " " << endl;
	}

  return 0;

}


and it gives me exactly what I need, thank you for your help!
Topic archived. No new replies allowed.