Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 8 should be cout, not cin.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Does this take up any RAM memory just because I have declared this number of
>> elements
Yes -- it will take up (1000000 * sizeof(string)) + sizeof(vector) before any of the strings have been initialized with anything.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>What happens to it's value once an element is deleted ?
The iterator is invalidated and you have to start it all over again. Example here: The program will crash after the erase() is executed unless the break statement is there to stop the loop.

#include <vector>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    vector<int> array;
    int i;
    for(i = 0; i < 10; i++)
        array.push_back(i);
    vector<int>::iterator it = array.begin();
    for(i = 0; it != array.end(); it++, i++)
    {
        if(i == 5)
        {
            array.erase(it);
            break;
        }
        cout << *it << "\n";

    }
    cout << "\n";
    for(it = array.begin(); it != array.end(); it++)
    {
        cout << *it << "\n";
    }
	return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The while statement isn't correct. infile won't read blank lines so you don't have to check for length of the word.

while( infile >> word )
{
   wordCount++;
}

case 11: That has a problem too: break exits the switch statement, not the while loop.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That's nice -- but where is the rest of your program? that doesn't show us what you did with the other cases.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It doesn't require any user imput. All you have to do is empty the file. But I would add another message that the file has been empied so that you can see that something happened.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

#3 is really nothing more than two lines of code -- open the file and then close it. Re-read my previous comments about this on how to open the file to make this work.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>line 4: test_string.length
Length is a function call, not a data object. test_string.length()

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 6: Use break instead of continue at the end of case statements.

>> cin >> text;
That only allows you to enter one word. If you want to enter more than one word there then you have to use getline(). There are two different versions of getline(), one for using on std::string objects and the other for character arrays

char text[255];
cin.getline(text, sizeof(text));

or
std::string text;
getline(cin, text);

>>it allows me to add only one word..for example if i wrote (do it)the program goes in an endless >>loop....but if i wrote(do)...it works..how can i fix this ?..

You'll have to post the code you wrote.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Read the link I posted

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>...cuz everytime i try to do this something goes wrong
Post the code you have tried.

>>so i know i'm bothering you
How do you know that??? I wouldn't have responded to your thread if I thought I was being "bothered" :) But I'm not allowed to write the code for you. You should be reading examples in your text book.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

#3 - empty a file. Look at the file flags link I posted then pick the one that will truncate a file to 0 length when the file is opened.

#6 - merge a file. I suppose that means to append one file to the end of another. This is related to menu #1. So what you do is open one file for input and the other for append then read the first file and write it to the second.

#7: just open a file for input then read each line and count the number of lines read.

When you get all that done you may know how to do the others.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The express editions of VC++ don't have a resource editor or compiler either but you can get free ones from google links. Dev-C++ doesn't have one either, I don't know about the other compilers Vernan mentioned.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

menu items 4 and 5 are probably the most difficult and I would leave them to last. But the others aren't all that hard.

Take #1 -- append text to the end of a file. The answer to that is all in how the output file is opened. Look at the list of open flags and read their descriptions. One of them, ate says that the text will always be written to the end of the file. That means the file would be opened like this: ofstream out("filename.txt", ios::ate); .
Now each time you write something to that stream it will be appended to the end of the file.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

VC++ 2008 Express is free for the downloading from M$. It doesn't support MFC, otherwise its nearly identical to the other paid versions. You can also download free Visual Studio C# and VB compilers.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Oh I see you want to store it in a character array with those leading 0's ? In c++ you would use stingstream class which works on strings just like fstream works on files

#include <sstream>
// other includes here
<snip>
int main()
{
    int number = 12;
    string line;
    stringstream stream;
    stream << setfill('0') << setw(4) << number;
    stream >> line;
    cout << line << "\n";

    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Padding integers with 0 to the left is a display thing only -- internally integers do not have leasing zeros. How to make them depends on how you are displaying them, such as are you using printf() or cout ?

int number = 12;
printf("%04d", number);

cout << setfill('0') << setw(4) << number << "\n";
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Neither flush() nor fflush() are used on input streams. Read this for how to flush input streams.

>>both do the same functions
Yes -- flush() is used by c++ streams in <fstream> and fflush() is used by C streams declared in <stdio.h>. Both flush the output stream.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>void InsertData(vector<Book> &Insert)
Nothing wrong with passing the entire array by reference if the function needs it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Pass the object by reference so that ChangeAdr() can change the original Book entry. The function is void so it does not return anything.

void ChangeAdr( Book& bk)
{
    getline(cin, bk.Address); 

    // This is where I come up short
    // How do I return the new adr. to Person[0]
//
// You don't -- that's what pass by reference does.
}

int main()
{
     vector<Book> Person;
    <snip>
    ChangeAdr( Person[0] );    
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>but tell me, how does one come to recognize something like that
Experience, practice, practice, and more practice -- thousands of hours of coding.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
void onedie::rollit()
{
	srand(time_t(NULL));
	result= 1 + rand() % 6;
}

Problem might be here -- move that srand() line to be one of the first lines in main() function. It should be executed only once during the lifetime of the program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

it's going to look an awful lot like the other two loops except with the allresults array isn't it?

probably

0012FBF0
Press any key to continue . . .

that was the result of the code from the last post

Ok, so I was wrong -- that is the address of the first element of the array, not its value.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>cout << allresults << endl;
That doesn't display the array but only the first element of the array. You need to create a loop and display each element one-at-a-time.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

AD, does that increment the different elements of the results array so you know which numbers are rolled how many times? I used the switch because that's what the prof has in the lesson plan, but I'm just as keen on keeping things simple.

I just looked at that switch statement and saw that it can be greatly simplified. Or something like this:

for (int i=0;i<rollsize;i++)
{
     int index = rolls[i] - 3;
     allresults[index] = allresults [index]+1;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you don't need that switch statement at all

for (int i=0;i<rollsize;i++)
{
        allresults[i] = allresults [i]+1;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>First off all; I apologize for my poor english skills, i'm from sweeden'
You are doing great so don't worry about it.

There are a couple ways to mondify the data in the vector

int main()
{
    vector<Book>  Person;
    Book x;
    cout << "Enter name: ";
    getline(cin,x.Name);
    cout << "Enter address: ";
    getline(cin,x.Address);
    cout << "Enter phone number: ";
    cin >> x.Phone;
    Person.push_back(x);

    // Now change the address
    cout << "Enter address: ";
    getline(cin,Person[0].Address);

    // Another way to change the adress
    vector<Book>::iterator it;
    it = Person.begin();
    cout << "Enter address: ";
    getline(cin, it->Address);



}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I got it to compile without error. Problem seems to be here

#ifndef THREEDICE_H
#define THREEDICE_H

#include "onedie.h" // <<<<<<<<<<<<<<<<<< ADD THIS LINE

class threedice
{
	public:
		void rollem();  	  
		int getsumFaces();
		
	private:
		onedie die1;
		onedie die2;
		onedie die3;
};

#endif
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If there's a large amount you could just attach the code. Hit the "Go Advance" button, scroll down to load attachments.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Post code. Its impossible for us to evalate the error messages without it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

void statistics (int rolls[];int rollsize; int rollresults []) You're supposed to use commas to separate the parameters, not semicolons. And if that line is the start of a function where are the { and } at the beginning and end of the function?

henpecked1 commented: very helpful, extremely patient +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

variables _score and _tempscore -- why use new to allocate only a single character? That seems like such as misuse of dynamic memory allocation. And those two variables appear to be used without ever initializing them to anything. Is there code that you didn't post that actually sets them to something ?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

IMO -- In My Opinion

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The first one only looks more efficient, but IMO the second one is because you can reference matrix just as you would any normal 2d array matrix[i][j] = 0;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>txt += INTTOCHARP(ballcount[0]);
In COUNTBALLS() -- INTTOCHARP() allocates memory with new but is never deleted.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can't write std::string objects to a binary file like that. The easiest way to simplify your program is to use a char array instead of std::string. Now in the class constructure initialize that char array to all 0s.

class Rivera
{
public:
	Rivera();
	
	void setKey(const int);
	int getKey() const;
	
	void setWord(const string&);
	string getWord() const;
	
private:
	int id;
	char palabra[13];
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

LOL AD when I said "how am I suppose to Know, its a figure of speech" I was talking about the fact that you said I called Narue a Dude when she is a female, and I said "how iam suppose to know"

I think Iam always being misunderstood on here, lets get back to the issues

Oh I see, sorry for the misunderstanding (generation gap I suppose). You can get a hint by her avatar -- it is wearing a dress. But that doesn't mean much either nowdays :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>how am I suppose to know, its a figure of speech
You are supposed to compile it then ask questions about what you don't understand. We aren't human compilers you know.

You DO have a computer (yes I know you do or you wouldn't be posting here) and a compiler (yes there are lots of free ones). So failing to do your own compilng is a very poor excuse.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

how about this AD

what does your compiler tell you?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

whatever dude you are just rude about it and you self proclaiming being the "Expert Meanie" says it all

Narue isn't a dude. Do you always call women that? And stop the flaming or you'll get hit with a stick and this thread will get closed.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>You wanted us to tell you exactly what was wrong with your code and how to fix it, it seems.
I agree -- maybe I should have just kept quiet, but I just can't help myself :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

only line 13 because lines 13 and 15 are identical, so it doesn't matter whether num % 2 is 0 or not.

and line 12 is wrong too because you used the = (assignment) operator instead of the == (boolean) operator.

Narue is attempting to get you to find the errors yourself. All the errors I pointed out (except the lines 12-15 problem) were probably reported by your compiler. Look at the first error, correct it, recompile then repeat this until all errors are fixed. Most compilers will tell you exactly where the errors are. But I must admit that sometimes (ok often) the error messages may be obscure and not tell you very much.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 10: Since Afile is an input file you can't use the << operator. So you probably meant to use cout instead of Afile to print the text on the screen.

lines 12 to 15: There is no difference between those conditional statements so you might as well not have the if/else clause at all.

line 14: else is mispelled -- there is no capital E in else.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

when I ran the program it worked as written except for changin <= 10 to < 10, as I stated in my oroginal post.
[edit]Oops! not it doesn't work right. [/edit]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

All you have to do is replact <= 10 to < 10 in each of the two loops for(int x = 1; x < 10; x++)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I already showed you the code to do it. Just paste that line after main().

#include <ctime>
// other includes here

int main()
{
   srand( time(0) );
   // other code here
}

The time() function gets the current system time in seconds since 1970. So every time you run the program time() will return something different.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you need to seed the random number generator. Most people see it with the current clock time of the computer

#include <ctime>

...
srand( time(0) );

Depending on your compiler you may need to typecast the return value of time() to size_t.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ancient Dragon..... I am new to this site and wud love to know how do u write the program to appear as like..... U know what I mean,

I'm afraid I don't know what you mean. appear as like what ? Do you mean the line numbers I added to your original post?

[code=cplusplus] // put your code here

[/code]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> i if i pass a pointer then i will have to receive it by a double pointer...else the change wont take place in the original pointer variable...

No, it doesn't work that way. You don't need to change the pointer itself, only the array inside the class. I know below isn't exactly how you are using it but illustrates how to use a pointer object. Note that function foo() is not changing the value of the pointer so there is no need to pass the array pointer by reference.

int foo( int* array)
{
   for(i = 0; i < 10; i++)
      array[i] = i;
}

int main()
{
    int array[10] = {0}
    foo(array);
    cout << array[0] << "\n";
    return 0;
}

In this example, the array pointer DOES need to be passed by reference because function foo() is changing it

int foo(int **array)
{
    *array = new int[10];
    for(int i = 0; i < 10; i++)
       *array[i] = i;
   return 10;
}

int main()
{
   int *array = 0;
   foo( &array );
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>needs a lot of work and practise
Yes, programming is probably the most time consuming course you will ever take, depending on how quickly you learn.

>>my lecture said we must read the textbook from cover to cover maybe 5 times but not less than that
You have a very good and competent instructor :)

Now tell me... if u said I must delete those statements which asking the user to make a selection where am I suppose to do that..... inside do... while.... I guess but how is the program gona know that when the key "1" is pressed had to go back and prompt the user to make a selection

Move lines 30 and 31 down a bit to inside the do loop, immediately after line 34.