Narue 5,707 Bad Cop Team Colleague

>iMas=new int(m);
You need to use square brackets instead of parens:

iMas=new int[m];

>for (int i=1; i<=m; i++)
Arrays are zero-based in C++. That means you start at 0 and stop at n - 1 or you'll be accessing memory that you're not allowed to access:

for (int i=0; i<m; i++)
Narue 5,707 Bad Cop Team Colleague

You can use the Application class from System.Windows.Forms (assuming we're talking about C++/CLI):

Application::Exit();
Narue 5,707 Bad Cop Team Colleague

So...did you even try to understand and correct the errors you're getting before posting all of that (without code tags)?

Narue 5,707 Bad Cop Team Colleague

>I have posted 3 threads requesting help on reading floating
>point values from a binary file but did not get any reply at all.
So you've flooded the forum with threads asking the same question? That alone will cause people to ignore you.

>Following code just throws an exception
What exception? That's somewhat important information.

Narue 5,707 Bad Cop Team Colleague

Is this a compilation warning or error? I'm not sure I understand what you mean by "I get this file popping up...".

Narue 5,707 Bad Cop Team Colleague

>can you please give me a little sample for this problem of mine
It depends on how you set up the dictionary for conversions. I'd probably go with a SQL database and have something like this:

Private Sub buttonConvert_Click ( ByVal sender As Object, ByVal e As EventArgs ) Handles buttonConvert.Click
    Me.Convert ( Me.textBoxKanji.Text )
End Sub

Private Sub Convert ( ByVal kanjiText As String )
    Dim storedProcedure As String = "kanji_to_katakana"
    Dim reader As SqlDataReader

    Try
        Dim connection As New SqlConnection ( Me.ConnectionString )
        Dim command As New SqlCommand ( storedProcedure, connection )

        command.CommandType = CommandType.StoredProcedure
        command.Parameters.Add ( New SqlParameter ( "@KanjiText", kanjiText ) )

        connection.Open()

        reader = cmd.ExecuteReader ( CommandBehavior.CloseConnection )

        Do While reader.Read
            Me.textBoxKatakana.AppendText ( reader.GetString ( 0 ) )
        Loop

        reader.Close()
    Catch ex As Exception
        ' Clean up and handle errors
        If reader IsNot Nothing AndAlso Not reader.IsClosed Then
            reader.Close()
        End If
    End Try
End Sub

The VB.NET code and stored procedure should be easy. The real work will be building the database of mappings. Alternatively you can just use a text file instead of a relational database.

>im not really familiar with kanji and katakana
You don't have to be. You can just get yourself a Kanji dictionary. They typically include translations to the other kana.

Narue 5,707 Bad Cop Team Colleague

>Is this possible in vb.net
Of course. It's not like you're doing anything exceptionally complicated, but it will be tedious. Kanji is symbolic and based on concepts/words while katakana is based on syllables. There's really not an algorithm for breaking kanji down into katakana, so you have to take all of the kanji you want to support and provide a mapping to the katakana translation.

Narue 5,707 Bad Cop Team Colleague

>but he said that I need to explain it better.
Tell him to ask a better question if he wants a better answer. Or you could tell me what context he's expecting and I'll help you understand the question better. In a general context, the best answer is another question: "Why do you think we do that?"

Narue 5,707 Bad Cop Team Colleague

>Why do we use memory heap to allocate classes, but not built-in types (int, char etc.)?
Bad question. Sometimes objects are better placed on the "stack", and sometimes they're better placed on the "heap". Provided the language doesn't force you into one or the other for internal reasons such as garbage collection, the answer to this question is very much dependent on context.

Narue 5,707 Bad Cop Team Colleague

>You're comparing colorCode to a character but defining colorCode as an integer!
Not a problem. char is an integer type and int can hold any value in the basic character set.

Narue 5,707 Bad Cop Team Colleague

Pure speculation, but maybe 10 grades per line, 5 lines per field, and each field separated by a blank line:

x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x

x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x

...
Narue 5,707 Bad Cop Team Colleague

>What I want to do is to put each line into an Array.

array<System::String^>^ a =
  gcnew array<System::String^> ( textBox1->Lines->Length );

a = textBox1->Lines;
Narue 5,707 Bad Cop Team Colleague

>1. How do I know what to initialize gradeArray to?
Well, SENTINEL wouldn't be a bad idea, and you can initialize it just like you do the frequency array. But you can get away with not initializing it at all, if you're careful:

i = 0; // Restart the gradeArray iteration

while ( i < MAX_SIZE_GRADE ) {
  int input;

  // Bomb out if we can't read anything
  if ( !( inFile>> input ) )
    break;

  // We'll assign the sentinel after the loop
  if ( input == SENTINEL )
    break;

  gradeArray[i] = input;

  if ( input >= 0 && input < MAX_SIZE_FREQ )
    ++freqArray[input];

  ++i;
}

// Make sure the grades are terminated
gradeArray[i] = SENTINEL;

I bet if you use that code, you'll find that the problem goes away.

>if I take out the freqArray[gradeArray]++ statement, the
>output for debugging statements show all the grades in the file
Think about it for a second. If you take that statement out, it doesn't crash. Could it be that you're hitting one of the problems I mentioned? Re-read scary #3 and think about what happens when you read the sentinel from your file.

>3. I guess my question then is how do I set up the frequency array
You're on the right track. But this experience should teach you that validating your assumptions is priority 1.

Narue 5,707 Bad Cop Team Colleague

>inFile.open("input.txt");
>outFile.open("output.txt");
Let's start here. You're not checking to see if the files were opened successfully. All kinds of gremlins can pop up if you use a file stream that failed to open.

>//Read input file and store into array
>while(gradeArray != SENTINEL)
You never initialized gradeArray, so gradeArray is garbage. You access a garbage value and expect predictable behavior.

>inFile>>gradeArray;
>freqArray[gradeArray]++;
This is scary on three levels:

  1. You don't check the input request for success
  2. You don't put a bound on i, so if the file has more than 50 numbers, you're overflowing the gradeArray
  3. You're using values from the file without checking their validity. This is also a potential overflow, but of freqArray this time.

That might help you pinpoint the problem more easily.

Narue 5,707 Bad Cop Team Colleague

In your project properties you can change the output directory. See if that does what you were expecting.

Narue 5,707 Bad Cop Team Colleague

>Is this possible to just move this debugfolder to c:\\ and debug it here all the time ?
>Is there any searchway you can change in C++ to make that work.
You're getting into questions that can't be answered without knowing what compiler you're using.

Narue 5,707 Bad Cop Team Colleague

>Is it possible to change this to only c:\MyApplication and put my project here instead ?
Sure, but it might not have the effect you want.

Narue 5,707 Bad Cop Team Colleague

>Does this meen that I will change this later when the application is finished ?
Yes and no. When you run your application outside of the debugger (assuming you're using an IDE), a relative path will probably use the current working directory to look for files. Assuming you can make sure that the current working directory is "C:\NewApplication\FileFolder", you don't need to change the code at all.

If you can't set the current working directory, you'll need to store a debug path and a release path for the file somewhere, and use it conditionally. For example:

#define DEBUG_PATH "Text.txt"
#define RELEASE_PATH "C:\\NewApplication\\FileFolder\\Text.txt"

#ifdef DEBUG_MODE
#define TEXT_PATH DEBUG_PATH
#else
#define TEXT_PATH RELEASE_PATH
#endif
ifstream OneFile ( TEXT_PATH );

Then when you compile, define DEBUG_MODE (or not), and everything will magically work.

Narue 5,707 Bad Cop Team Colleague

I'd suggest always surrounding blocks with braces until you understand the rules. Properly indenting your code according to how your compiler sees things would produce this:

if(choiceMenuInput==1)
    input();
else if(choiceMenuInput==2)
    return 0;
else
    printf("\n\n\n\n\n ERROR YOU HAVE ENTERED A WRONG CHOICE\n\n\n");

system("pause");
inputedit();

The last two lines are not part of the else clause. You need to wrap the whole thing in braces:

if(choiceMenuInput==1)
    input();
else if(choiceMenuInput==2)
    return 0;
else {
    printf("\n\n\n\n\n ERROR YOU HAVE ENTERED A WRONG CHOICE\n\n\n");
    system("pause");
    inputedit();
}
Narue 5,707 Bad Cop Team Colleague

>But what about the newlines and/or tabs?
Indeed. And what about such monstrosities as "\t \n\n\t"? Does "extra whitespace" refer to adjacent whitespace characters of the same value, any value that isspace returns true for, or are we only working with the ' ' character? The problem doesn't seem to be well defined.

>I have read about peek() and putback().
Unnecessary. You can save the last character read from the stream and use it as a state to process the next character:

char last = 0;
char ch;

while ( in.get ( ch ) ) {
  if ( ch == ' ' && last == ' ' )
    continue;

  out.put ( ch );
  last = ch;
}

A good rule of thumb is that if you think you need to peek or putback on a stream, you probably need to improve your design.

Narue 5,707 Bad Cop Team Colleague

>What is the use of using namespace std; ?
Namespaces are like drawers in a filing cabinet. All of the standard library is in one drawer, and the only way to get to the stuff in that drawer is to tell C++ where to find it. You do that by prefixing the name with std:

#include <iostream>

int main()
{
  std::cout<<"Hello, world!\n";
}

std::cout is basically like saying "Hey C++! Look in std and get me cout , asap!". If you don't tell C++ which drawer to look in, it won't find what you want. That's why this gives you a nasty little error, C++ is complaining that it can't find cout:

#include <iostream>

int main()
{
  cout<<"Hello, world!\n";
}

Now, it's somewhat tedious to keep typing std:: in front of everything, so you can also tell C++ to assume what drawer to look in. A using declaration tells C++ to assume that the declared name is in the declared namespace:

#include <iostream>

int main()
{
  using std::cout;

  cout<<"Hello, world!\n";
}

The using std::cout part tells C++ to assume that wherever you say cout , you really mean std::cout .

Even that can get tedious, and maybe you want to say "Hey C++! Pretend that everything is prefixed with std:: ! On the double!" You can do that with a using directive:

#include <iostream>

int main()
{
  using namespace std;

  cout<<"Hello, world!\n";
}

The using namespace std part is a quick and easy way …

vmanes commented: Nicely stated. +2
Narue 5,707 Bad Cop Team Colleague

A stringstream is still a stream, you can't just copy a stream as std::vector does for its elements. Try using a pointer/smart pointer if you really want a vector of stringstreams.

Narue 5,707 Bad Cop Team Colleague

>Sorry but you did not get the message Narue .
Don't apologize, restate your question so that somebody understands what you want.

jbennet commented: :) your answer was perfect +24
Narue 5,707 Bad Cop Team Colleague

>I need to know why the designer of this website put in the software development
Because people have questions about software development.

>And why is designer of this page must to put web deve...
Because people have questions about web development.

>i do not know why do i need php or asp
Then your question isn't about this forum, it's about development in general, and how to select a language for your project. Unfortunately, that comes with experience. Start writing programs and you'll quickly learn what languages are suitable for your needs.

>I really do not get it why designer of this page must put all these programs
Daniweb caters to a wide range of people.

Narue 5,707 Bad Cop Team Colleague

C doesn't have templates at all, so both class and typename are unique to C++. They're also both standard, but class is an older form[1], so you're likely to see it more often. There are various personal preferences for how to use it; my preference is to use typename pretty much exclusively.

[1] class was used originally because the designers didn't want to add a new keyword to the language. But when a new keyword was needed for other things (typename), it was basically overloaded to handle this job as well.

Narue 5,707 Bad Cop Team Colleague

The first is legal, the second is not. But if you meant typename rather than typedef, the two are functionally identical.

Narue 5,707 Bad Cop Team Colleague

>what's the problem on the second line???
There are two problems:

1) Technical
The C++ standard specifies that the main function must return an integer. It has never returned void, as C++ is based on C, and C has never returned void from main. The void main issue was born of C dialects before it was standardized, and by that time too many people already had the habit of using void main. Thus, some compilers decided to support this style for convenience, which set a backward compatibility problem where void main will never go away.

By using void main, your code is not portable beyond the compiler you wrote it with. That's the real problem, but if you had no intention of portability, the only problem is personal (see below).

2) Personal
The problem on the second line (in that it shows the worth of the entire program) displays an elitist view of programming and a general disdain for anyone who doesn't hold language standards as the word of God. Using the problem on the second line to avoid reading the rest of the code is occasionally used as an excuse to avoid facing one's lack of understanding of other people's code. However, it's more often used as a weapon by language snobs to dismiss the efforts of beginners or anyone with whom the snob has a difference of opinion.

Narue 5,707 Bad Cop Team Colleague

Use sprintf to convert the number to a string and then print all but the first character:

sprintf ( buffer, "%d", value );
puts ( &buffer[1] );
Narue 5,707 Bad Cop Team Colleague

>if(t=n)
Note that in C++ the == operator is for comparison, = is for assignment.

Narue 5,707 Bad Cop Team Colleague

>My question is, is there a way of proving it with code?
Well, that's different from your original question, isn't it? Why not perform the operations a few billion times and see which one takes longer?

#include <ctime>
#include <climits>
#include <iostream>

template <typename T>
double runassignadd ( unsigned long n )
{
  T a = T();
  T b = T();
  std::clock_t start = std::clock();

  for ( unsigned long i = 0; i < n; i++ )
    a += b;
  
  return ( (double)std::clock() - start ) / CLOCKS_PER_SEC;
}

template <typename T>
double runadd ( unsigned long n )
{
  T a = T();
  T b = T();
  std::clock_t start = std::clock();

  for ( unsigned long i = 0; i < n; i++ )
    a = a + b;
  
  return ( (double)std::clock() - start ) / CLOCKS_PER_SEC;
}

int main()
{
  std::cout<<"A += B    -- "<< runassignadd<int> ( UINT_MAX ) <<'\n';
  std::cout<<"A = A + B -- "<< runadd<int> ( UINT_MAX ) <<'\n';
}

>Otherwise how do we know for sure.
Don't put too much weight on empirical tests for making a general statement. There are often unknowns that skew the result and depend on the machine you test with, the code you use, and so on.

Narue 5,707 Bad Cop Team Colleague

>Is there any way of proving that the += operator is more efficient than the + operator...
Sure. Look at it and say "Hey, A is being evaluated twice with the + operator but only once with the += operator" and "Hey, A + B is probably making a temporary copy".

Narue 5,707 Bad Cop Team Colleague

>the first example appears to be a DOS based C++
>programming library due to the <conio.h> header file
conio.h is available outside of DOS-based C++. What you should be looking for is <iostream.h>, which says that the program is expecting to use pre-standard C++. If you see <iostream> instead (without the .h), the program at least recognizes standard C++.

>Upon implementing cin.reset(), (if it actually exists, which I'm
>not entirely convinced) it seems that you might need a header file?
cin.reset() (assuming it exists, which it doesn't) is a member function of cin. So you would only need the header that includes the cin object, which is <iostream>.

Narue 5,707 Bad Cop Team Colleague

>Could it maybe exist in the world of Visual C++?
It could, just like cin.imalittleteapot(); could exist as well. If your professor is teaching you compiler extensions as if they were standard C++, I'd question her competence.

>Unfortunately most of what you discussed in your very
>through post is a little beyond my scope right now.
It works toward an advanced solution. You only need to read the first quarter of the thread for the two simple and common solutions.

>I need to reset the input steam after it goes into failure.
Not meaning to oversimplify, but it goes into failure because there are unexpected characters in the stream. The only way to deal with that is to remove the unexpected characters (ie. read and discard everything you can) so that the stream buffer is empty.

>When she says "reset cin" does she mean to clear the
>stream status flags, in essence, "reseting" the input stream?
Probably, now that I think about it.

Narue 5,707 Bad Cop Team Colleague

>Can anyone shed some insight on
>cin.reset()? Does it really exist?
No. By "reset", hopefully your professor meant for you to flush it.

>Am I worrying about the exact wording of my prof's algorithm too much and just use
You are, but he also didn't really communicate the task to you very well. Your solution is quite likely what he wanted, and you can improve it by reading the thread I linked to above.

Narue 5,707 Bad Cop Team Colleague

>i want it to cycle through the array and organize it from smallest to largest.
Yea, that's a really bad idea. First, it's likely to be inefficient for an operation that people expect to be constant. Second, unless you want to limit your operator to read-only operations, you can expect havoc to ensue when you sort and then the client code overwrites an index they thought was empty but isn't any longer. For example:

#include <algorithm>
#include <iostream>

class Whatever {
private:
  int m_value[10];
public:
  Whatever() { std::fill ( m_value, m_value + 10, 0 );  }
  int& operator[] ( std::size_t index )
  {
    std::sort ( m_value, m_value + 10 );
    return m_value[index];
  }
};

int main()
{
  Whatever blah;

  blah[5] = 123;

  for ( int i = 0; i < 10; i++ )
    std::cout<< blah[i] <<' ';
  std::cout<<'\n';

  blah[2] = 45;

  for ( int i = 0; i < 10; i++ )
    std::cout<< blah[i] <<' ';
  std::cout<<'\n';

  blah[8] = 9; // Oh noes! We just killed 45

  for ( int i = 0; i < 10; i++ )
    std::cout<< blah[i] <<' ';
  std::cout<<'\n';
}
Narue 5,707 Bad Cop Team Colleague

>I must ask if it is considered good manners to say thanks to
>both of you, or do I just mark as solved with no comment?
It's polite to say thanks, but not necessary. And you should only mark the thread as solved when you have no more relevant questions. ;)

>I would have thought that the failure to read would generate an error.
It does generate an error, but you don't do anything with that error immediately and the error doesn't halt operation by default. Assuming your file consists of complete records, the error occurs here:

infile>>thisdate;

And you do all of this before even looking at the state of the stream (which would tell you that an error occurred):

dates.push_back(thisdate);
for(int i=0;i!=5;i++){
	infile>>thisnumber;
	frequency[thisnumber-1]++;
	numbers.push_back(thisnumber);
	infile>>dash;
}
infile>>thisnumber;
frequency[thisnumber-1]++;
numbers.push_back(thisnumber);
count++;

When the stream is in an error state, it doesn't do anything. Any request for input will return immediately and the variables you're writing to retain their original values. That's why thisdate, thisnumber, and dash all keep the same values when trying to read thisdate fails. infile is in an error state after that and any request for input is effectively a no-op.

By "generate an error", I assume you mean throw an exception. The iostream library doesn't do that by default, but you can tell it to, which is another solution to the problem:

#include <fstream>
#include <iostream>

int main()
{
  std::ifstream in ( "test.txt" );
  int value; …
Narue 5,707 Bad Cop Team Colleague

It's generally a good idea to establish a context before asking a question. Your question looks more like it should be a reply to an existing thread than the starting post of a new thread...

Narue 5,707 Bad Cop Team Colleague

>I believe the problem is somehow with while(infile),
>ie, I dont believe I understand exactly how it works.
Your intuition is correct. while ( infile ) is using infile's implicit conversion to void* to check for an error state. If the conversion returns a null pointer, it means the stream is in an error state, otherwise it's fine. End-of-file counts as an error state, by the way. Your problem is with the order of operations. Here's what happens with a 2 line file:

1) Check the state of the stream -> good
2) Read a record successfully
3) Process the last successfully read record

4) Check the state of the stream -> good
5) Read a record successfully
6) Process the last successfully read record

7) Check the state of the stream -> good
8) Fail to read a record due to end-of-file
9) Process the last successfully read record

10) Check the state of the stream -> bad, terminate the loop

The bold part is your problem. What you're doing is checking the state of the stream before trying to read a record and using that to control your loop. The error state of the stream is only set after you've tried and failed to read a record. You need to read the record first.

Ancient Dragon's solution uses the result of reading a record to control the loop, which will work properly because if the read fails, the stream is …

Narue 5,707 Bad Cop Team Colleague

>Does anyone know how to delete a specific index of an array?
The only way is to overwrite it by shifting every element after the one you want to delete over it. For example:

for ( int i = to_delete + 1; i < n; i++ )
  array[i - 1] = array[i];
--n;
Narue 5,707 Bad Cop Team Colleague

A stringstream is still a stream. You can seek on it:

#include <iostream>
#include <sstream>

int main()
{
  std::stringstream sin ( "123456789" );
  int value;

  sin.seekg ( 2 );
  sin>> value;

  std::cout<< value <<'\n';
}
Narue 5,707 Bad Cop Team Colleague

The easiest way is to use the time library:

#include <ctime>
#include <iostream>

int main()
{
    std::time_t temp = std::time ( 0 );
    std::tm *first = std::localtime ( &temp );

    first->tm_year = 2008 - 1900;
    first->tm_mday = 1;
    first->tm_mon = 0;

    if ( std::mktime ( first ) != (std::time_t)-1 ) {
        char weekday[100];

        if ( std::strftime ( weekday, 100, "%A", first ) > 0 )
            std::cout<<"The first day of the year is "<< weekday <<'\n';
    }
}
Narue 5,707 Bad Cop Team Colleague

>Simply doing the following does not seem to work because it returns a runtime error #2
I have no idea what your compiler thinks runtime error #2 is, but you can't assign a pointer to an array, which is precisely what you're trying to do with myArray = new int[i_Count]; . The two types are incompatible.

>This gave me an assertion error.
That's no surprise, actually. You're deleting the original memory in your function, but because the function parameter is a copy of the original pointer, you're effectively doing this:

void myFunct(int *a_myArray)
{
     // Create a memory leak
     new int[9];

     // Create a dangling pointer
     delete [] a_myArray;
}

To actually change the original pointer, you need to pass a reference to it:

void myFunct(int*& a_myArray)
{
     // new array
     int *a_myArray2 = new int[9];

     // random code for filling array
     //...

     // delete old and create new
     delete [] a_myArray;
     a_myArray = a_myArray2;
}

My recommendation is to avoid arrays and simulated arrays entirely and just use a vector object:

#include <vector>

int main()
{
  std::vector<int> v ( 5 );

  //...

  v.resize ( 9 );

  //...
}
Narue 5,707 Bad Cop Team Colleague

The only one I know of is absolutely ancient, and it doesn't seem to be available on MSDN anymore. There are plenty of books on the subject, but if you want online freebies, you'll have to do some netdiving on google.

Narue 5,707 Bad Cop Team Colleague

>So are spaces and null characters similar?
No, not at all.

>how can i differentiate between the two
You don't need to differentiate between the two. You need to figure out why your code is stopping when you don't think it should. We can help, but you need to post more code. Preferably a complete program that exhibits the problem.

Narue 5,707 Bad Cop Team Colleague

I don't know of any. The communities that support illegal software are typically full of script kiddies who aren't capable of or interested in writing something new, and communities that have qualified members typically find illegal software offensive. You're basically asking for two mutually exclusive features.

p.s. For anyone else interested in answering this question, naming a community and/or linking to it would likely be in violation of Daniweb's policies.

Narue 5,707 Bad Cop Team Colleague

Amazingly enough, it is.

Narue 5,707 Bad Cop Team Colleague

Um, just set the PasswordChar property of the password text box to '*'.

Narue 5,707 Bad Cop Team Colleague

>Except for the facts like C++ has more keywords, so int new;
>will not compile in C++, but it will work without any problem in C.
The statement I was quoting talked about functional ability, not piddling differences between syntax and semantics. For example, C++ can indeed be used to build an OS. Everything you can use C for, you can also use C++ for, provided C++ is supported on the target system.

Narue 5,707 Bad Cop Team Colleague

>what is the point of solving practice problems if nobody coments on your code?
Practice problems aren't for other people, they're for you. You solve them to practice your skills and gain insights. If someone comments on your code, that's merely icing on the cake.

Generally you learn more by doing. The fact that you found some of your own errors means that you're learning, and the fact that you acknowledge your code needs improvement means that you'll push to improve it. I'm afraid I don't see what your problem is.

Concerning your code: at a glance I don't see any glaring errors and the code is consistent and well formed. To pick out any real problems I would have to go out of my way to test and debug it. Presumably you haven't gotten any comments because others came to that conclusion as well and didn't have the time to properly critique your program.

Narue 5,707 Bad Cop Team Colleague

I'm sorry, I refuse to answer your question until you stop using silly chat speak to communicate.