Help with char array output

This is a discussion on Help with char array output within the C++ Programming forums, part of the General Programming Boards category; So I've been stuck on this for a while. The assignment is to read in a certain input to a ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    4

    Help with char array output

    So I've been stuck on this for a while. The assignment is to read in a certain input to a char array, do some stuff and then output it in a char array. But as I understand it, we cannot output each index using a loop. I was wondering how to go about this.

    Here is my code so far for the program even though I know parts of it are wrong and you probably won't understand what I'm doing unless you read the requirements. Basically we are not allowed to do a LOT of things so that's why the code is how it is.

    Code:
    #include <iostream>
    using namespace std;
    
    const int MaxString = 73;
    typedef char str[MaxString];
    
    void main()
    {
    	char command_step[25];
    	char motor_step[49];
    	char motor_program[73];
    	char output[81];
    
    	const str prompt = "MOTOR CONTROL PROGRAM GENERATOR";
    	const str command = "Command Step:";
    	const str motor = "Motor   Step:";
    	const str program = "Complete Motor Program:";
    	const str hyphen = "------------------------------------------------------------------------";
    	const str equals = "========================================================================";
    	const str pound = "##";
    	const str invalid = "Invalid command step!";
    
    	int num = 0;
    
    	cout << prompt << endl << endl;
    
    	for (;;)
    	{
    		cout << command;
    		cin.getline(command_step, 25, '\n');
    	
    		if ((command_step[0] == NULL) || (command_step[0] == ' '))
    		{
    			cout << hyphen << endl;
    		}
    		else if (command_step[0] == '0')
    		{
    			cout << motor << command_step << endl;
    			cout << hyphen << endl;
    
    		        output[0] = command_step[0];
    		        output[1] = command_step[1];
    
    			cout << command;
    			cin.getline(command_step, 25, '\n');
    			cout << hyphen << endl << endl;
    
    			if ((command_step[0] == NULL))
    			{
    				cout << program << output << pound << endl << endl << equals << endl << endl;
    			}
    		}
    		else if (command_step[0] == '1')
    		{
    			if (command_step[2] != '.')
    			{
    				cout << invalid << endl;
    				cout << hyphen << endl;
    			}
    			else
    			{
    				for (int i = 0; i < 2; i++)
    				{
    					motor_step[i] = command_step[i];
    					cout << motor_step[i];
    				}
    				
    				//cout << motor << motor_step << endl;
    				//cout << hyphen << endl;
    			}
    		}
    		else
    		{
    			cout << invalid << endl;
    			cout << hyphen << endl;
    		}
    	}
    }
    You can ignore from the second else if on unless you get what I'm trying to do and can offer suggestions on the rest of the program. If you don't get what I'm trying to do then I'll try to explain better. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    void main() -> int main()

    I have no clue what this program is supposed to do, but I guess I don't need to know. It would be helpful though if you could tell us more exactly what your problem is?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    Sorry about that. This program is supposed to read in a line of input from the user and keep reading until the user inputs nothing. It then validates the input and concatenates them together into one output array.

    For example:

    23.4.5 of user input means that there are 2 parameters (the 4 and the 5), the 3 doesn't mean anything but you need to keep it, the 4 means that you need to output 4 1's (1111) and the 5 means output 5 2's (22222). So the whole thing would be 23111122222. You save that in the output array and if the user inputs something else then you go through the process again and add it on to the end of the output array. If the user inputs nothing, then you output whatever is in the output array and add 2 pound signs (##) onto the end to signify the end of the output.

    65. would not be valid, 1.2 would not be valid (the first number has to have 2 digits), 34.8 would not be valid since there are not 3 parameters, 02 is valid and outputs just 02.

    We're not supposed to use any functions except cout << endl, cout << a char array, and cin.getline().

    We can't cout the char array one element at a time though. That's what I'm having trouble with as well as the overall program.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    Quote Originally Posted by chimaera523 View Post
    We can't cout the char array one element at a time though. That's what I'm having trouble with as well as the overall program.
    Seeing as this apparently is a school assignment, I'm not sure if I want to give away anything. But I could tell you this; there's no need to output char arrays one element at the time if you want to output the entire array. See if you can make anything out of the following...:

    Code:
    #include <iostream>
    int main()
    {
      char s[11] = "1234567890";  // why s[11], why not s[10]?
      std::cout << s << std::endl;  // what does this output, and why?
    
      s[1] = '\0';  // what does this do?
      std::cout << s << std::endl;  // what does this output, and why?
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    Well I know that you can just use the name of the whole array to output it. My problem is that you have to copy from the input array into a different output array and when I try to output the output array without using each element I get the things in the array plus some weird symbols and stuff. Is that just garbage in the rest of the array since it's not filled?

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    That's exactly it. You have to put the null character ('\0') at the end of the string in the copying process to prevent outputting garbage. I suspect this is where your problems lie?
    Code:
    for (int i = 0; i < 2; i++)
    {
        motor_step[i] = command_step[i];
        cout << motor_step[i];
    }
    motor_step[2] = '\0'; // what if we add this line?

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    4
    So simple yet so profound haha. Thanks very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 01:02 PM

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