Requesting help for programming/ CS majors?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Superfreak04
    D7 Elite Keymasher
    • Jan 2007
    • 2407

    #31
    Re: Requesting help for programming/ CS majors?

    Originally posted by Kibblre
    In case you hadn't been able to translate rushy's code, here's what part one could look like:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char name[50];
    	int count = 0;
    	bool valid = true;
    	char inputChar; 
    
    	cout << "Enter a name one letter at a time and denote the end of the name with *: ";
    
    	while(valid)
    	{
    		cin >> inputChar;
    		if(inputChar != '*')
    		{
    			name[count] = inputChar;
    			count++;
    		}
    		else
    			valid = false;
    	}
    
    	system("pause");
    	return 0;
    }


    Finding the size of a char array in C++ is sizeof(name).

    Also, you might want to look into including the * as well for your later parts so you know to stop sorting there. Or make a new array using the count variable, but you'd have to make the new char array a pointer since you can't declare variable size char arrays unless you do.
    Hmm, I used this code and what happens is that I can type and hit as many letters as I want. Then once I type in, then it will end. Makes sense, although I'm supposed to make it ask me to enter a character each time I want to enter a new one.

    "Enter a name one letter at a time and denote the end of the name with *: E

    "Enter a name one letter at a time and denote the end of the name with *: R

    "Enter a name one letter at a time and denote the end of the name with *: I

    "Enter a name one letter at a time and denote the end of the name with *: C

    etc.

    At least, that's how we've been taught. It's so that we can make sure it's working properly I guess.

    Comment

    • shenjoku
      Wubalubadubdub
      • May 2005
      • 1697

      #32
      Re: Requesting help for programming/ CS majors?

      Originally posted by rushyrulz
      damn, C++ is so much nicer at handling IO than other languages I've programmed in..
      Do you know if there's any way to restrict input to just one character, Kibblre?
      You can. You call std::cin() with std::setw(), passing it the number of characters you're expecting like this:

      Code:
      #include <iomanip> // Need this for std::setw()
      
      char c;
      std::cin >> std::setw(1) >> c;
      This will only read in the first character input by the user. This will not limit them on what they can input though.
      boop

      Comment

      • Kibblre
        Caelondia Represent
        • Jul 2004
        • 1984

        #33
        Re: Requesting help for programming/ CS majors?

        Originally posted by Superfreak04
        Hmm, I used this code and what happens is that I can type and hit as many letters as I want. Then once I type in, then it will end. Makes sense, although I'm supposed to make it ask me to enter a character each time I want to enter a new one.

        "Enter a name one letter at a time and denote the end of the name with *: E

        "Enter a name one letter at a time and denote the end of the name with *: R

        "Enter a name one letter at a time and denote the end of the name with *: I

        "Enter a name one letter at a time and denote the end of the name with *: C

        etc.

        At least, that's how we've been taught. It's so that we can make sure it's working properly I guess.
        Then you'd just have to move the cout statement from where it is to directly above the cin statement. That way it'll print out every time the loop restarts. So the while loop would look like:

        Code:
        	while(valid)
        	{
                        cout << "Enter a name one letter at a time and denote the end of the name with *: ";
        		cin.get(inputChar);
        		if(inputChar != '*')
        		{
        			name[count] = inputChar;
        			count++;
        		}
        		else
        			valid = false;
                   
                        cout << endl; //put and end line here for better readability
        	}
        Originally posted by shenjoku
        You can. You call std::cin() with std::setw(), passing it the number of characters you're expecting like this:

        Code:
        #include <iomanip> // Need this for std::setw()
        
        char c;
        std::cin >> std::setw(1) >> c;
        This will only read in the first character input by the user. This will not limit them on what they can input though.
        Yup, now I remember. I haven't used setw since last year and I totally forgot about it.
        Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

        Comment

        • dAnceguy117
          new hand moves = dab
          FFR Simfile Author
          • Dec 2002
          • 10097

          #34
          Re: Requesting help for programming/ CS majors?

          here's a sketchy solution for step 3 using C-style everything (which seems to work? based on the sample code?) and keeping rushy's variable names.

          no guarantees about just plugging this in, but you should be able to see what your objective is here.

          Code:
          char letter_a = 'a';
          int value_a = (int)letter_a;
          
          int distribution[26];
          
          for (int i=0; i<26; i++)
          {
          	distribution[i] = 0;
          }
          
          for (int i=0; i<name.size(); i++)
          {
          	int value_myLetter = (int)name[i];
          	int elemNumber = value_myLetter - value_a;
          	
          	distribution[elemNumber]++;
          }
          and then uhhh you're gonna want to print it.

          Comment

          • Kibblre
            Caelondia Represent
            • Jul 2004
            • 1984

            #35
            Re: Requesting help for programming/ CS majors?

            Originally posted by Superfreak04
            I have to use dummy's because the PC won't display anything without it.
            Not sure if you caught this yet looking at my code, but that's what system("pause") will do for you.

            Also, what level course is this? If this is an introductory course then he's making you do some tricky stuff for that level.
            Last edited by Kibblre; 10-15-2013, 03:23 PM.
            Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

            Comment

            • Superfreak04
              D7 Elite Keymasher
              • Jan 2007
              • 2407

              #36
              Re: Requesting help for programming/ CS majors?

              Originally posted by Kibblre
              Not sure if you caught this yet looking at my code, but that's what system("pause") will do for you.

              Also, what level course is this? If this is an introductory course then he's making you do some tricky stuff for that level.
              Yeah I noticed that right away lol. And yes this is intro. ;_;

              Comment

              • Kibblre
                Caelondia Represent
                • Jul 2004
                • 1984

                #37
                Re: Requesting help for programming/ CS majors?

                Question about part 2: does he want capitals included too? It'll make it even harder if you have to sort lower case letters combined with capital letters instead of making them all lowercase. If you were to use standard sorting methods, the capitals would sort themselves to the beginning in order then the lower case letters would be in order, like ABCDabcd
                Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                Comment

                • Superfreak04
                  D7 Elite Keymasher
                  • Jan 2007
                  • 2407

                  #38
                  Re: Requesting help for programming/ CS majors?

                  I believe she wants us to include capitals. Even though it's up to the user whether or not you wanna type in a capital.

                  Comment

                  • Kibblre
                    Caelondia Represent
                    • Jul 2004
                    • 1984

                    #39
                    Re: Requesting help for programming/ CS majors?

                    Originally posted by Superfreak04
                    I believe she wants us to include capitals. Even though it's up to the user whether or not you wanna type in a capital.
                    So then you'd want the output like I described? It'll be tough to make it aAbBcCdD
                    Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                    Comment

                    • Air En Trance
                      FFR Player
                      • Oct 2013
                      • 17

                      #40
                      Re: Requesting help for programming/ CS majors?

                      this assignment is a horrible way to learn arrays and input

                      Comment

                      • Kibblre
                        Caelondia Represent
                        • Jul 2004
                        • 1984

                        #41
                        Re: Requesting help for programming/ CS majors?

                        Originally posted by Air En Trance
                        this assignment is a horrible way to learn arrays and input
                        Pretty much, yeah. Should've just stopped at the inputting the name part and then have them print it.
                        Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                        Comment

                        • Superfreak04
                          D7 Elite Keymasher
                          • Jan 2007
                          • 2407

                          #42
                          Re: Requesting help for programming/ CS majors?

                          Originally posted by Kibblre
                          So then you'd want the output like I described? It'll be tough to make it aAbBcCdD
                          Yeah lets do that just for safety.

                          Comment

                          • Kibblre
                            Caelondia Represent
                            • Jul 2004
                            • 1984

                            #43
                            Re: Requesting help for programming/ CS majors?

                            Originally posted by Superfreak04
                            Yeah lets do that just for safety.
                            Good. For the record, this is because of ASCII values. If you were to compare 'a' and 'b', you'd be comparing 97 and 98. Capital letters range from 65 to 90 while lowercase range from 97 to 122.
                            Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.

                            Comment

                            • Fission
                              no
                              FFR Simfile Author
                              • Jan 2004
                              • 1850

                              #44
                              Re: Requesting help for programming/ CS majors?

                              i'm not sure why C idiom solutions are being suggested when the OP specifically mentioned C++. it's a terrible idea to mix and match them, don't do it.

                              Comment

                              • rushyrulz
                                Digital Dancing!
                                FFR Simfile Author
                                FFR Music Producer
                                • Feb 2006
                                • 12985

                                #45
                                Re: Requesting help for programming/ CS majors?

                                Originally posted by Kibblre
                                If this is an introductory course then he's making you do some tricky stuff for that level.
                                I've had much harder assignments in introductory level classes. One of my projects was to code a sieve of eratosthenes from scratch.


                                Comment

                                Working...