views:

83

answers:

3

My task consists of two parts. First I have to create globbal char array of 100 elements, and insert some text to it using cin. Afterwards calculate amount of chars, and create dedicated array with the length of the inputted text. I was thinking about following solution :

char[100]inputData;

int main()
{  

   cin >> inputData >> endl;

   int length=0;
   for(int i=0; i<100; i++)
   {
           while(inputData[i] == "\0")
           {
               ++count;
           }
   }
char c = new char[count];

Am I thinking good ?

Second part of the task is to introduce in the first program dynamically created array of pointers to all inserted words. Adding a new word should print all the previous words and if there is no space for next words, size of the inputData array should be increased twice. And to be honest this is a bit too much for me. How I can create pointers to words specifically ? And how can I increase the size of global array without loosing its content ? With some temporary array ?

A: 

Global arrays can't have the size changed dynamically unless they are a pointer to an array, in which case you can erase them and reallocate them.

Perhaps what you're after is an automatically resizing array, like a std::vector. You can see how many letters you have in the array by calling size() on the vector, and you can increase the size of the array by calling resize().

While not the most elegant solution, it might be a bit easier to use for the moment.

SalamiArmi
A: 
#include <iostream>
#include <string>

int main()
{
    std::string input;
    std::getline(std::cin, input);
}
FredOverflow
+2  A: 

Regardless of the rest of your question, you appear to have some incorrect ideas about while loops. Let's look at this code.

for(int i=0; i<100; i++) {
    while(inputData[i] == "\0") {
        ++count;
    }
}

First, "\0" is not the NUL character. It is a pointer to a string containing only the terminating NUL byte. You want '\0' instead. Assuming this change, there are still problems. Let's work through what will happen:

  • How does a while loop work? It executes the body repeatedly, as long as the condition is true.
  • When does a while loop finish? When the condition is finally made false by executing the body.
  • What's the condition of your loop? inputData[i] == '\0', after correction.
  • What's the body? ++count.
  • Can ++count ever change the value of the condition? No, because it doesn't change i.
  • So, if inputData[i] is not the NUL byte, the while loop never executes.
  • But, if inputData[i] is the NUL byte, the while loop executes forever.
  • Assuming you've read a proper string into inputData, then at some point inputData[i] will be NUL, and you'll have an infinite loop.

To count the length of a standard C string, just do this

count = strlen(inputData);

If for some reason you really have to write a loop, then the following works:

int len = 0, 
while (inputData[len] != '\0') {
    len++;
}

After the loop, len holds the length of the string.

Dale Hagglund
no it wont. It might do if it said != '\0'
pm100
Whoops. Edited to fix.
Dale Hagglund