dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Strings basics

  1. #1
    Join Date
    May 2004
    Posts
    249

    Strings basics

    Guys I am very confused about what I am doing in this piece of code. my aim is to print out the days of the week, which is stored in a string. There seem to be errors that I have never come across

    I would like to avoid vectors in this case. However, the use of pointers is ok.

    Below is my code

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    
    using namespace std;
    using std::string; //I understand thsi could be omitted. 
    
    const int ARRAY_SIZE = 7;
    
    int main ()
    {
    	const string dayOfWeek[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    	int fValue = 0; //to b used in the future
    
    	for (int i = 0; i < ARRAY_SIZE; i++)
    		{
    			//cout << dayOfWeek.at(i) << endl;
                            cout << dayOfWeek[i] << endl;
    			
    		}
    
    
    
    	return 0;
    }
    What can be done to improve my code. I am Using VS 2013 Express...
    --------------------------------------------------
    Please pardon me for having bad English.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,559

    Re: Strings basics

    Looks okay to me. What's the problem?

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: Strings basics

    What can be done to improve my code
    Consider
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	const string dayOfWeek[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    
    	for (const auto& dow : dayOfWeek)
    		cout << dow << endl;
    
    	return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  4. #4
    Join Date
    May 2004
    Posts
    249

    Re: Strings basics

    I get the following error messages

    Code:
    cout << dayOfWeek.at(i) << endl;
    Name:  err1.JPG
Views: 141
Size:  17.5 KB

    Code:
    cout << dayOfWeek[i] << endl;
    Name:  err2.JPG
Views: 242
Size:  24.1 KB
    Last edited by rockx; October 14th, 2015 at 08:20 PM.
    --------------------------------------------------
    Please pardon me for having bad English.

  5. #5
    Join Date
    Jun 2015
    Posts
    208

    Re: Strings basics

    Quote Originally Posted by rockx View Post
    I get the following error messages
    Note that dayOfWeek is a string array, not a string.

    When you do,

    Code:
    dayOfWeek.at(i); // accessing the i'th char of a string
    you are assuming dayOfWeek is a string which it isn't so you get an error, whereas when you do

    Code:
    dayOfWeek[i]; // accessing the i'th string of a string array
    you are assuming dayOfWeek is a string array which it is so it's ok.

    ---

    Regarding "using namespace". I never use it, neither for C++ library namespaces nor for my own namespaces. I think fully qualified names have an important signal value. You instantly see where a name belongs. So my preferred code would look like this (inside an application specific namespace somewhere),

    Code:
    const std::string dayOfWeek[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    
    for (const auto& day : dayOfWeek) {
        std::cout << day << std::endl;
    }
    Since the for-loop iterates just one line I might consider removing the braces but then I would make it a one-liner like this,

    Code:
    for (const auto& day : dayOfWeek) std::cout << day << std::endl;
    So either braces (K&R indent style) or a one-liner. In my experience this reduces the risk of mistakes.

    But all of this is a matter of personal taste. It's what I do and others do what they do.
    Last edited by tiliavirga; October 15th, 2015 at 04:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)