Gonbe 32 Newbie Poster
for (i=0; i<Number_entrys;i++)
{
    scanf("%s",names);
}

You are looping here to obtain "number_entrys" entries, but you do not use your counter. Change it to this:

for (i = 0; i < Number_entrys; i++)
{
    scanf("%s",names[i]);
}

I'm also not sure what the goal is of this statement:

printf("%s",names);

But be aware this likely won't print anything truly useful. If you want to print all names you should loop over the 2-dimensional array.

Gonbe 32 Newbie Poster

You were overwriting the last character in the string with a '\0' yourself. You didn't notice it on smaller input because fgets reads the newline character as well. So it overwrote that instead of a character of the line you entered.

Here's quick fix of the code. The problem was in your GetString function. I didn't look closely to the rest of the code but if you need help with anything just let me know. (Though it seems it does now what you want it to do!)

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void GetString (char line[], int size);
void counter  (char line[], int counts[]);
void PrintCounts(int counts[]);

/* Define the array size */
#define MAX_WORD_LENGTH (20)

/* This function gets the string from the user and saves it to the array line. This function also gets the size of the array passed in from the main function */
void GetString (char line[], int size)
{
    size_t input_length = 0;

    printf("Enter a string no longer than %d characters:\n", MAX_WORD_LENGTH);
    fgets(line, size, stdin);

    // This isn't needed. if fgets reads 'size - 1' chars it automatically
    // terminates the array with a '\0'. Note that fgets also reads the '\n' though.
    // So i replaced this line:

    // line[strlen(line) - 1] = '\0';

    // with this:
    input_length = strlen(line);

    if (line[input_length - 1] == '\n')
    {
        line[input_length - 1] = '\0';
    }
    // Still things in the input buffer.
    else
    {
        while (fgetc(stdin) != '\n');
    }

    printf("You Entered %s\n", line); …
Gonbe 32 Newbie Poster

It's indeed true that 1 character is reserved for the '\0' character so when declaring an array of size 20 it is capable of storing a string of length 19 at most. I'm not sure why your stored one of length 18. Can you post a small code example that is representative of the issue?

Gonbe 32 Newbie Poster

Typical case of TL;DR but I did notice the following words:

A text-based menu driven program should be created.

Could try there I guess.

Gonbe 32 Newbie Poster

How about you first create a class representing a linked list that contains some basic operations?

  • Insert a value at a given index.
  • Remove a value at a given index.
  • Obtain the size
  • Get the value at a specified index ([] operator?)
  • Remove the first entry matching a supplied parameter.
  • Count the amount of occurrences of a supplied element.

and so on. You probably only need the first 4 anyway.

Gonbe 32 Newbie Poster

well to do it on paper you would go through and take the frist letter, count how mant time that letter occurrs and record, then move on to the second letter count how many times it is used then record.

What would you do on a string like "aaaaaa"? Would this be counted as 36 a's (6 times 6) by your algorithm?

I kinda get what needs to be done im just not sure how to do it. Im thinking a "for" loop will be used to loop the array and everytime a character is done the loop moves onto the next in the string.

That's a viable option. Looping over every character in the string is probably useful. Then what will you do? Considering what you described above, how do you plan on storing the count for each character? How are you going to record what characters you've already processed?

Gonbe 32 Newbie Poster

let me know if anyone has any pointers thankS!

0x0F021016
0x0E031418
0x08051033

Ba dum tss

any help would be greatly appreciated!!! thanks!

Anyway, at a quick first glance I don't really see anything that could cause errors. What have you established yourself already? At what point do problems arise?

-edit-

Actually, replace

cout<<grid[x,y];

with

cout<<grid[x][y];
Gonbe 32 Newbie Poster

Hmm no this doesn't quite work. Let's start with words. We a C-String (array of character termined by a '\0') that we obtained from the user. Now we want to count how often each character occurs in that string.

How would you attempt to do it? Explain it conceptually; how would you do it if you had to do it when the word was written down and you only had a pencil and a notepad? Don't get into programming terminology.

Once you have a clear view on that we can attempt to convert the idea into a working application.

Gonbe 32 Newbie Poster

Because that's a different sorting algorithm. For small amounts of data or data that is already nearly sorted insertion sort is as good and fast as most others, maybe faster. IMO It's also the easiest to code and remember.

It's not inserting the element at 'i' into the 'already sorted' section though. Instead of going through all elements of an array using 'i' and then doing a swap for every element 'j' that follows it if it's smaller than 'i', going through the remains and only remembering the lowest value and afterwards doing the swap doesn't really seem different to me. Only that you swap less but the principle is exactly the same.

Gonbe 32 Newbie Poster

Your sort is a substitution sort

Ah yes, I read over the code to quickly I guess. I've never actually used this one myself (and I don't think I ever will =P) but ensuring the current index is the smallest by repeatedly swapping elements further in the array with it if they are smaller seems a bit odd.

Why isn't the smallest value in the remaining section only looked up and then swapped with index i at the end?

Gonbe 32 Newbie Poster

length = strlen(line); in the first module has an error "implicit conversion looses integer precision"

strlen returns a size_t officially which is generally an unsigned int. I attempt to store it into an int which could theoretically cause issues. You'd be best to change it yeah, my bad.

int symbol_count[ALPHABET_SIZE] = {0}; has an "unused variable" error

That's your compiler being strict. It's more of a warning.

readin(line, MAXSIZE);
update_character_count(symbol, occurrences, size);
counter(line,count);

This is indeed not what I had in mind when writing that function. Your "counter" function was used to process each individual character in the read string. It currently only prints if it's alpha or not, but this could easily be extended to count the occurrences for each character. That is where you could use the function I supplied, or could come up with your own solution.

The array that is currently unused just happens to have 26 counters, which is the amount of character in the alphabet.

The function I supplied wants a char, array of occurrences and the size of that array and it will update the counter for the supplied symbol if and only if that symbol is a-z or A-Z. I didn't knew your exact needs but I assumed that 'a' and 'A' should be considered as 2x'a' in your case.

Let me know how it works out.

P.S.

char symbol;
int occurrences, size;

I'm not sure why you added these global …

Gonbe 32 Newbie Poster

int &count

That is not allowed in C. Reference parameters are C++, use a pointer instead.

const int MAXSIZE = 100;
char line[MAXSIZE] = {'\0'};

I think this falls under "variable length arrays" (VLA's) which is a C99 feature. I'm not entirely sure because of the const keyword though, but I think it's still one. Define MAXSIZE as a preprocessor macro instead.

void readin(char line[])

As a guideline always supply the capacity of an array as well. In this case I'll add one called 'max'.

scanf("%s", line);

This will not read the whole line. If the line contains spaces for example, it will stop earlier. Replace it with the following:

// fgets reads until a newline is encountered (which it
// will store in 'line' OR 'max-1' characters have been read.
fgets(line, max, stdin);

length = strlen(line);

// The whole string fit into our array, remove the newline
if (line[length - 1] == '\n')
{
    line[length - 1] = '\0';
}
// We couldn't fit all of the input into the array. Clear the input buffer.
else
{
    while (fgetc(stdin) != '\n');
}

As far as your counter function goes, I think you can figure that out yourself. A little addition that might help you on how to do it:

void counter(char line[], int count)
{
    int symbol_count[ALPHABET_SIZE] = {0};

    while(line[count])
    {
        if(isalpha(line[count]))
            printf("Character %c is alphabetic\n",line[count]);
        else
            printf("character %c is not alphabetic\n",line[count]);
        count++;
    }
    return;
}
Gonbe 32 Newbie Poster

Try to do it with 1 array first. There is no added value when working with two, and with your code you lose track of how many elements A contains. Your code also doesn't compile. (You call swap with 2 arguments while it has 3 for example)

Try to explain in words what you're trying to achieve. Looks like you read something about bubble sort, but wording it might help you understand it better yourself too. (Quack!)

Gonbe 32 Newbie Poster

Hmmm... I think i'd start like this, just an idea though.

int main(void)
{
    return 0;
}
Gonbe 32 Newbie Poster

It's more of a hint/possible solution towards what you need. As mentioned in the post it's something I quickly wrote down to show the idea of using a string you manipulate. It should work for an arbitrary bit count though, although I'm sure performance-wise it could be improved on.

You may use it, sure, but if you don't understand what's going on I don't see much of a point. I'll let you decide what you think is best for yourself.

-edit-

gonbe i tried to modify ur code for my problem but could't achieve, what i wanted

You should only have to change the define from 6 to 48. If that doesn't result in what you want I don't think I understand your problem correctly. If you set 48 it will take forever to finish, but that is to be expected with 2^48 strings that have to be calculated and printed. The logic of it should work. I'm not sure if I have time tomorrow to try to fix your existing code, it will probably be monday at the earliest if at any time. Try to figure out in the meanwhile what's going wrong and post your attempt(s). The bits increase, but that doesn't matter too much for the application's logic.

HunainHafeez commented: gonbe i tried to modify ur code for my problem but could't achieve, what i wanted, so if you can possibly make any changes within my code to make it work for 48 bits then please resolve it, i wasted a whole lot of time on it, Help would be appreciated de +0
Gonbe 32 Newbie Poster

Such C++ includes in C code. The way you do it will become problematic when using 48 bits as the amount of possible permutations is huge. You don't have to store them though and just output them as they get calculated. Another thing that might become an issue is the use of counters (depending on how you do it). You could use an unsigned long long which is guaranteed to be 64 bits but it's a C99 standard feature. I'd go for re-writing the alghorithm though.

Depending on what your goal is exactly you could use a string. A quick attempt using a recursive method would result in something like this:

#include <stdio.h>

#define BIT_COUNT (6)

void print_bits(int bitcount, char* bitstring, int offset)
{
    if (bitcount == 0)
    {
        printf("%s\n", bitstring);
    }
    else
    {
        bitstring[offset] = '0';
        print_bits(bitcount - 1, bitstring, offset + 1);

        bitstring[offset] = '1';
        print_bits(bitcount - 1, bitstring, offset + 1);
    }
}

int main()
{
    char bits[BIT_COUNT + 1];
    bits[BIT_COUNT] = '\0';

    print_bits(BIT_COUNT, bits, 0);

    return 0;
}
Gonbe 32 Newbie Poster

It is because of this line:

scanf("%c", &yesno);

This will also read '\n' when pressing return in addition to whatever you entered. On the next iteration this will still be in the buffer so that statement will read that directly. It fails the input check so the next iteration is started which once again asks for input. This time the buffer would be empty. (if you only entered 1 character)

Replace that line with this (for example..):

scanf("%c", &yesno);

// Clear the input buffer, if needed.
if (yesno != '\n') while (getchar() != '\n');

-edit-

Add one space in scanf(" %c", etc.), and all will be well. The space in the format string, tells scanf() to go past any whitespace, including a newline.

While that works when inputting what's needed it will still cause undesired behaviour when entering "hello world" for example. It's not directly your question I guess, kieranrea, but it's something to keep in mind.

Gonbe 32 Newbie Poster

It's fine to use. math.h might define M_PI but this is only if

__STRICT_ANSI__ 

is defined. As a result some implementation might not have it either I think. If it does have it and it's defined, you wouldn't be able to define M_PI yourself when including math.h unless you use shady compiler-specific intructions to temporarily disable the definition like this:

#include <stdio.h>
#include <math.h>

int main(void)
{
    #pragma push_macro("M_PI")
    #undef M_PI
    double M_PI = 1.00;

    printf("%f\n", M_PI);
    #pragma pop_macro("M_PI")

    return 0;
}

While supported by quite some compilers I do not recommend this, just rename your variable if needed.

Gonbe 32 Newbie Poster

Look into WinSock when programming on a windows platform and BSD Sockets for Linux.

You can also use Boost.Asio.

Beej's socket guide is also a well-written source to learn how sockets work for a beginner.

iamthwee commented: +1 +14
Gonbe 32 Newbie Poster
What does the &arr[i] do? I think it has something to do with pointers but I don't understand pointers either so I'm just trying to get the basics...

You are (kind of) correct. '&' as a unary operator is used to obtain the memory address of a variable. For example, say you have the following declaration:

int number;

The type of 'number' is 'int'. The type of the following:

&number;

would be 'int *'. Which means "a pointer to an int" and you may interpret this as 'the memory address where "number" is stored'.

What is the use of it? Well, one use is that you needn't copy the value of number when passing it to a function. What's more important though is that is allows you to return multiple values from a function. For example:

void func (int val)
{
    val = 20;
}

int main(void)
{
    int a = 10;

    func(a);

    // a is still 10 when printed here, because it's passed "by value"
}

This is probably what you're used to. But if you work with pointers it would look like:

void func (int* val)
{
    (*val) = 20;
}

int main(void)
{
    int a = 10;

    func(&a);

    // a is 20 here as it was passed as a pointer. (by reference)
    // We passed the memory location of 'a' which is copied by value
    // but the memory address itself is ofcourse unchanged meaning 
    // any changes …
Gonbe 32 Newbie Poster

There's quite a bit wrong in your code.

  1. Note that 'int' is for storing integers. You almost seem to want to use it as an enumeration (enum keyword) but you don't initialize the values. When querying for input using '>>' it will try to read an integer because the type is int. if you want to read text you should use "string" instead.

  2. Comparing values is done with the '==' operator, not with the '=' (assignment) operator.

    if ( YesNo = Yes) // if user answers yes

This will assign the value of 'Yes' to 'YesNo' and this expression will return the value of 'Yes'.

  1. The idea of naming options bound in a certain catagory isn't bad, it's generally actually quite good. (for readability) You would use an enumeration for that though. It looks as follows (look it up):

    enum YesNo { Yes, No };

You would then use YesNo as a type. In this specific case I would not create an enum though as C++ has the 'bool' type just for cases like this. It can be 'true' or 'false'.

  1. As far as logic goes it seems somewhat alright. You really miss a loop though. Look for "for" and "while". In pseudocode you program could look as follows:
Let the user enter it's number. (0-100)
Set the computer's guess to 50 the lower boundary to 0 and upper boundary to 100.

As long as the user didn't say the number is guessed:

    Ask the …
Gonbe 32 Newbie Poster

Thanks jallen and Andrew ( for explaining it very clearly )

You should note that their answers are wrong though. The outcome of the expressions cannot be determined.

Lucaci Andrew commented: Indeed. +5
Gonbe 32 Newbie Poster

so i think i should only change some if in the code and it will work correctly

No change should be needed as I check for whitespaces, not for spaces. If multiple whitespaces may occur in a row you will have to adjust the code although it's minor. If you need help let me know.

but now my problem is how to include the file inside a 2D array.

You're going in the right direction. You should however watch out with how many times your loops iteratie. They both iterate one time too many now. You either have to replace '<' with '<=' or remove "+1".

As far as fscanf() goes, it requires a format string and memory addresses to place data in. The function description I linked contains information about character combinations possible and what they do. In your case you'll want:

fscanf(file, "%d", &(myarr[k][i]));
Gonbe 32 Newbie Poster

You can either implement your own classes to deal with it, although if building something serious (that requires performance) you're likely better off using an external library. A couple that come to mind:

TTMath
GMP

Gonbe 32 Newbie Poster

You can't say anything useful about the code, only that it results in undefined behaviour; you're writing to the same variable twice or more without an intervening sequence point.

See section J2 and section 6.5 here.

-edit-

Looked up the exact point in section J2 I wanted to refer to:

Between two sequence points, an object is modified more than once, or is modified
and the prior value is read other than to determine the value to be stored (6.5).

Then, from section 6.5 (expressions):

Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.

Then from Annex C one of the definitions of a sequence point:

The end of a full expression: an initializer (6.7.8); the expression in an expression
statement (6.8.3); the controlling expression of a selection statement (if or switch)
(6.8.4); the controlling expression of a while or do statement (6.8.5); each of the
expressions of a for statement (6.8.5.3); the expression in a return statement
(6.8.6.4).

Gonbe 32 Newbie Poster

I think the two pass solution is cleaner and would be easier to understand for a beginner.

Probably. Not sure about performance though especially if you want to consider rows not necessarily have the same amount of items. (You'd have to allocate every row size to the size of the row containing most elements. You'd then have to track the amount of occupied slots per row or work with sentinal values which might not also be possible. (although he does only mention positive integers)

To keep it simple, here's example code you can use to read in a file with the folowing format:

1859 1974 726 1684 3048 6243
1853 1896 763 1738 3298 6250
2011 1715 877 1647 3548 6250
2125 1769 648 1685 4213 6227
2047 1858 661 1681 4463 6247
2272 1839 486 1653 4713 6250
2219 1843 497 1616 4963 6175
1679 1420 1234 1901 5648 6234
1522 1851 1060 1817 5898 6250

More specifically the code assumes the following for correct functioning:

  • Every row contains an equal amount of numbers.
  • Every line ends with a newline. (to be more realistic I allowed the last line to not end in a newline)
  • Values are seperated by a single whitespace. (space or newline)

I did not assume the amount of rows is equal to the amount of columns as this is not the case in your example either. Many of these points are easy to adjust but they would make the code …

Gonbe 32 Newbie Poster

It doesn't need to know because solving a big problem is solved by solving subproblems in recursion. You might get confused because there's a print statement between two recursive calls.

Try to visualize what the recursive steps look like. For example:

inorder(p->lchild);

When would this recursive call print something? The first print statement would take place on the lowest node in the tree because every recursive call makes this call before printing it's node value and then processing the right side.

Gonbe 32 Newbie Poster

I am not exactly sure what you mean but if I interpret it correctly you are right:

When deleting a node from a tree you basically replace it with the most-left node from it's right subtree. And why is that? Well per definition every node in the right subtree is bigger or equal than the node you want to delete and everything to the left is smaller. This goes for every node. So the most left node in the right subtree will always be the smallest value in that subtree (Another way of putting it is to say that every other node in the right subtree is bigger or equal to this one!) but it's still bigger or equal than the node you want to delete. This means every node in the left subtree is still smaller so the rules stand.

Note that this is just the way to do it when a node has 2 subtree's. With 0 or 1 subtree deleting a node is easy.

Gonbe 32 Newbie Poster

You'd have to resize your 2-dimensional array as you read the file. Initially you should allocate the array dynamically using malloc(). Then read from the file and when you run into array boundaries expand it using realloc().

Gonbe 32 Newbie Poster

Hmm bit-wise operators don't seem to be blacklisted. You could implement binary long division with them.

Gonbe 32 Newbie Poster

This should work:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

struct CALL_DATA
{
    string list;
    int number;
    int lcmonth;
    int lcyear;
};

int main()
{
    int i = 0;
    CALL_DATA listd[100];

    ifstream file ("list.txt");

    if (file.is_open())
    {
        while (file.good())
        {
            char temp = 0;

            getline(file, listd[i].list);

            // The file could have ended with a newline symbol meaning the last line was empty.
            // I didn't knew how you created your file so I added this check. You can remove it if needed.
            if (listd[i].list.length() != 0)
            {
                file >> listd[i].number;
                file >> listd[i].lcmonth;
                file >> listd[i].lcyear;

                while (file.get() != '\n' && file);

                cout << "List: " << listd[i].list
                     << "\n\tnumber:  "  << listd[i].number
                     << "\n\tlcmonth: " << listd[i].lcmonth
                     << "\n\tlcyear:  "  << listd[i].lcyear
                     << endl << endl;

                i++;
            }
        }
        file.close();
    }

    cout << "Amount of entries read: " << i << endl;

    return 0;
}
Gonbe 32 Newbie Poster

Yes, and I like turtles.

-edit-
In case it isn't clear: What is your question exactly??...

Gonbe 32 Newbie Poster

"ok"..

Gonbe 32 Newbie Poster

How did you declare your function?

The function/prototype should be defined as:

void computeOrder(int& spoolOrderAmount, int& spoolStockAmount, double shippingHandling = 10.00);

and the implementation as:

void computeOrder(int& spoolOrderAmount, int& spoolStockAmount, double shippingHandling)
{
    // code
}
Gonbe 32 Newbie Poster

While I don't necessarily agree with creating a function to compare integers his suggestion is that you have a string-like compare. You get two numbers and if the first is smaller than the second the function returns -1, if they are equal it returns 0 and otherwise it returns 1.

I wouldn't create a function for that personally though as it doesn't really add anything in my opinion. You're not adding the result to something directly so you'd still have to branch on it's result. I'd go for creating functions of which you can convince yourself it makes sense to have them as functions in the first place and/or because it can be identified as an individual process in the entire application's workings.

Gonbe 32 Newbie Poster

You commented your code with some steps. You could say your program does the following:

  1. Intialize random seed.
  2. Generate secret number.
  3. Play guessing game. (keeps starting guess attempts until guessed, tracks amount of tries)
  4. Do guess attempt. (either right or wrong, involves asking the user for a number)
  5. Obtain number from user.

You might be able to find more subtasks or create a different list but the aim is to find subtasks and place their logic in seperate functions that call eachother.

Gonbe 32 Newbie Poster
  1. You declare "census city[10];" within the loop while it's also declared outside of it..
  2. The issue is similar to your other thread. See my awnser there: http://www.daniweb.com/software-development/c/threads/437105/scanf-is-skipping-in-last-input-.-if-i-use-int-instead-of-charcode-is-runn
  3. Use fgets() instead of gets().

Summary of the problem: With the first scanf a newline remains in the stream. gets() stops when reading a newline which is the first thing it reads now because it was left in the stream as mentioned.

Gonbe 32 Newbie Poster

It's not skipping input, you already supplied it. Say you enter 1 for the 1st number. What you really enter is:

'1' and '\n'.

The '1' is read into 'a' and the '\n' remains in the stream. When calling scanf() a next time it reads the newline character that was left in the buffer.

There's a couple of ways to solve this, one being:

#include<stdio.h>
#include<math.h>

main()
{
    char a,m;

    printf("1st\n");
    scanf("%c",&a);

    fflush(stdin);

    printf("2nd\n");
    scanf("%c",&m);

    return 0;
}

It's generally a bit shady to flush inputs streams though. Your goal is to ensure the buffer is empty before the next scanf(). You could also do something like this:

#include<stdio.h>
#include<math.h>

char read_character (void)
{
    char result = 0, symbol = 0;

    // Read the first character.
    scanf("%c", &result);

    // Read until you encounter a newline.
    while (symbol != '\n')
    {
        scanf("%c", &symbol);
    }

    return result;
}

main()
{
    char a = 0,m = 0;

    printf("1st\n");
    a = read_character();

    printf("2nd\n");
    m = read_character();

    return 0;
}

Another option would be to read a stream using fgets() and then obtain the first character from that. The idea remains the same.

-edit-
To awnser why it does run when using int instead of char is that when using an int a newline isn't a valid value for it so it's skipped.

Gonbe 32 Newbie Poster

The result of "pow(2,31)" is a double. You apply a bitshift operation on it, which is not possible. Replace

pow(2,31)

with

((unsigned int)pow(2,31))

and it should work.

You don't need pow to begin with though, you don't have to start from the most significant digit as you're just counting bits. You could therefore also replace

if(num &((pow(2,31))>>i))

with

if(num & (1 << i))
Gonbe 32 Newbie Poster

I believe you made a topic about binary trees before and your case was a binary search tree right? (for everynode the condition holds that every node in it's left subtree is smaller than the node's value and every node in the right subtree is bigger or equal)

You should be able to figure out how to do this. Do it without thinking in code first, do it on paper. You could even draw an example tree if it helps you.

You will want to maintain the property mentioned earlier. For example, say you have this tree:

       8
     /   \ 
    3     10
   / \     \
  1   6     14
     / \    /
    4   7  13

And we delete the node with value 3. The tree would transform as follows:

      8
    /   \
   4     10
  / \     \
 1   6     14
      \    /
       7  13

I'll leave up to you to figure out what happened and more importantly why it happened.

Gonbe 32 Newbie Poster

it does them nothing but get them further behind in their lessons.

If he mindlessly copy pastes it without even trying to understand what's going on I have no guilty conscience about that. People should be intelligent enough to make the best of a given answer.

But yeah, had this discussion in the 'C' section before and I don't plan on repeating it. Downvote me for it or get my post removed by an admin/moderator if it upsets you. You have my permission.

Gonbe 32 Newbie Poster

Hmm vague code is vague. I made a quick adjustment based on what i THINK you're trying to do here:

#include <stdio.h>
main()
{
    char a[4][4];
    int row,col;

    // Note that the body of this loop is executed only twice.
    // Is this what you want?
    for (row=0;row<4;row=row+2)
    {
        for (col=0;col<4;col++)
        {
            // Note that this condition is always true.
            // You're basically saying "something it equal to something else, or it isn't!"
            if ((row==col)||(row!=col))
            {
                //row index 0 and 2 get 'x'.
                a[row][col]='x';
            }
        }
    }

    // I assume you want to print the two rows you entered data for here.
    // (Why not do it directly, you wouldn't need the array? I assume you want
    //  to develop this further into something else..)
    // I modified the increment section. It goes through EVERY row now so not only the 2
    // you entered data for.
    for (row=0;row<4;row++)
    {
        // You only entered data for row 0 and 2.
        // So skipping the contents of the others now (while still printing the newline)
        if (row % 2 == 0)
        {
           for (col=0;col<4;col++)
            {
                printf ("%c",a[row][col]);
            }
        }

        // Note I added a newline as I THINK you want each row in the 2D array on it's own line.
        printf("\n");
    }
}

This would print the following:

xxxx

xxxx

If this is not what you want, provide more information..

Gonbe 32 Newbie Poster

Sleep() takes a parameter that represents milliseconds, not microseconds. I just wanted to point that out in case readers don't study the documentation you linked to closely enough, and then find Sleep() doesn't have the granularity they want.

Woops yeah, meant to type milliseconds. Thanks for the heads up.

Gonbe 32 Newbie Poster

One thing to look out for is integer division. the return type of fact, x and n are all integers. Try this:

#include<iostream>

using namespace std;
int fact (int no)
{
    int total;
    for (int i=0; i<=no; i++)
    {
        if(i ==0)
            total = 1;
        else
            total = total * i;
    }
    return total;
}
int main()
{
    int x,n;
    double result;
    cout<<"Please key i x value : ";
    cin>>x;
    cout<<"\n";
    cout<<"Please key in n value : ";
    cin>>n;
    if (x<n)
        cout<<"Wrong input, x valur must be greater than n !!\n"<<endl;
    else
        cout <<"\nResult = 1 +2!/"<<x-2
             <<" - 3!/"<<x-3
             <<" + 4!/"<<x-4<< " - "<<n
             <<"!/"<<x-n
             <<" = "<<1+(fact(2)/static_cast<double>(x-2))-(fact(3)/static_cast<double>(x-3))+(fact(4)/static_cast<double>(x-4))-(fact(n)/(static_cast<double>(x-n)));

    return 0;
}
Gonbe 32 Newbie Poster

To show how important it is you specify clearly what you want, another partial solution (which is less flexible and can't detect everything but might fit your needs) would be the following:

#include <stdio.h>
#include <conio.h>

int main (void)
{
    for (;;)
    {
        // A key has been pressed, but not yet read.
        if (kbhit())
        {
            // Read it and show us something.
            printf("Key pressed: %d.\n", _getch());
        }
    }

    return 0;
}

I'll leave it up to you to take what you need and modify it to include timestamps.

Gonbe 32 Newbie Poster

What kind of keyboard hits? Do you want to detect any form of keypress? Does it have to happen while another activity is going on? Be more specific.

It's likely (it depends on what you want exactly) that it's not possible in a platform independent way. Assuming you develop on Windows you could for example have something like the following:

#include <stdio.h>
#include <windows.h>

int main (void)
{
    HANDLE            handle       = GetStdHandle(STD_INPUT_HANDLE);
    KEY_EVENT_RECORD *currentEvent = NULL;
    DWORD             eventCount;
    INPUT_RECORD      inputRecord;

    // Run this example endlessly.
    for (;;)
    {
        // Determine if anything can be read
        PeekConsoleInput(handle, &inputRecord, 1, &eventCount);

        // See if there was an event
        if(eventCount > 0)
        {
            // Read it.
            ReadConsoleInput(handle, &inputRecord, 1, &eventCount);

            currentEvent = &(inputRecord.Event.KeyEvent);

            // Filter for key events and only display it for the key being down.
            // Option for filtering the holding of a key are limited so omitted here.
            if (inputRecord.EventType == KEY_EVENT && currentEvent->bKeyDown)
            {
                printf("Detected key-press: %d. (%c)\n", currentEvent->wVirtualKeyCode,
                                                         currentEvent->uChar.AsciiChar);
            }
        }
    }

    return 0;
}

Using this, I assume you could extend it so it records timestamps and calculates the difference between them? (And if not, ask here and mentioned specifically what isn't working)

Gonbe 32 Newbie Poster

@Gonbe - Do you think it would be better for someone trying to learn to get an informed nudge or suggestion rather than a working solution? Often times a full solution only serves to further bury a person struggling with the basics.

Potentially. In this case I was confused with the actual question as the solution is very trivial and hardly "nudgeable". The problem would be:

1) No idea how to read 3 numbers. => very easy to google
2) No idea how to determine the highest/lowest. => It's very easy to develop an algorithm that does it as you'd do it on paper. The only issue I could imagine here would be not knowing how to translate that into code which boils down to not knowing how boolean operators/branching work. (assuming math libraries may not be used. Function knowledge might help as well but not needed)

So yeah, in this case I posted the/a solution as the question is, as mentioned, trivial and I wouldn't be able to nudge him towards anything he wouldn't easily be able to find himself.

Gonbe 32 Newbie Poster

Do you mean something like this:

#include <iostream>

using namespace std;

int min (const int a, const int b);
int max (const int a, const int b);

int main()
{
    int a, b, c;

    cout << "Enter three numbers: " << endl;
    cin >> a >> b >> c;

    cout << "Lowest: "  << min(min(a,b), c) << endl
         << "Highest: " << max(max(a,b), c) << endl;

    return 0;
}

int min (const int a, const int b)
{
    return (a < b) ? a : b;
}

int max (const int a, const int b)
{
    return (a > b) ? a : b;
}

Or is there some sort of catch?

Gonbe 32 Newbie Poster

The code in itself is quite poor. In addition it's not clear to me what the program SHOULD do. Can you explain what the input is supposed to represent, when and which scores should be elinated and other relevant information? When entering the values you supplied the application outputs:

Assignment 2 first score has been eliminated.
Assignment 3 second score has been eliminated.

Which I guess is what you don't want. I don't understand the logic of your application though.. If I were you I'd re-write it from scratch and set it up better. A good start would be is to identify subproblems that could be contained in a function.

Gonbe 32 Newbie Poster

A high "cool story bro" factor on this one.. You omit information and you do not provide any attempts done by yourself or even own thoughts on how to fix it.

You could follow the primary-school like methods of multiplication e.g.

 18
 18
 -- *
144    // 8*8 = 64. Write 4 and remember 6. 8*1 = 8, Add remembered 6 = 14.
18     // Next number from the buttom. Start number 1 to the left. Rest is identical.
--- +
324    // Add the subresults to obtain the final results. Empty slots are 0.