Malfunctioning Getch

This is a discussion on Malfunctioning Getch within the C++ Programming forums, part of the General Programming Boards category; Ok, so I use 8 for the array size and almost every problem goes away - except it still doesn't ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,889
    Ok, so I use 8 for the array size and almost every problem goes away - except it still doesn't pause for the getch.
    Tested your code (modified to use the standard headers) on the MinGW port of GCC 3.4.2, and the program works as I would expect it to.

    So then, other than cin, is there any other functions that could pause the program for me? Having to stop it with cin seems, I don't know, kind of primitive...
    getch() is rather 'primitive'. If you want something more 'advanced', you can #include <limits>, then write:
    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    The cin.ignore() discards whatever characters may remain on the buffer, then cin.get() waits for the enter key to be pressed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    It says that qualifier std is not a class or namespace name... do I just have an old limit library, or is there something else I"m missing?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,889
    It says that qualifier std is not a class or namespace name... do I just have an old limit library, or is there something else I"m missing?
    hmm... perhaps you could test with:
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    	string s;
    	cout << "Please enter your first name followed by a newline\n";
    	cin >> s;
    	cout << "Hello, " << s << '\n';
    	return 0; // this return statement isn't necessary
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    41
    Y don't u replace getch() with cin.get();

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Does Borland 5 even support new-style headers? Does this compile?
    Code:
    #include <iostream>
    
    int main()
    {
      std::cout << "Hello, World!\n";
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Quote Originally Posted by CornedBee
    Does Borland 5 even support new-style headers? Does this compile?
    Code:
    #include <iostream>
    
    int main()
    {
      std::cout << "Hello, World!\n";
    }
    Apparently not... still the same "Qualifier 'std' is not a class or namespace name." And I tried just the cin.get(), that did as much as the getch - still just ran right through the end without pause.

    And if I put in the using namespace std line, it gives an error of "Namespace name expected."

    ...

    Oh, this is interesting. If I write in the other line of that test segment with the using namespace std, by inserting #include <string>, it gives me a completely different set of errors: "'cin' is not a member of 'std'," "'streamsize' is not a member of 'std'," and two other errors around the two-line alternative to getch suggested that I suppose derive from these...

    Dunno if that helps at all, but anything's worth it. After all, I've got two days of school left to figure this out!

  7. #22
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    The '98 C++ standard was only included in version 5.5.

    A workaround can be found here for your 5.02 version
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,889
    If I write in the other line of that test segment with the using namespace std, by inserting #include <string>, it gives me a completely different set of errors: "'cin' is not a member of 'std'," "'streamsize' is not a member of 'std'," and two other errors around the two-line alternative to getch suggested that I suppose derive from these
    I see. What I suspect is that <limits> is not implemented by your pre-standard compiler, or is implemented out of the std namespace. As such, when you #include <limits>, std is not defined, so the compiler complains.

    Then, it looks like your compiler did implement std::string in the std namespace, and that is included when you #include <string> (as is standard). But when you first triest a using namespace std directive, you did not include that standard header, so again the std identifier was not defined.

    If you cannot use the workaround that Mario F. suggested, you can go for a poor man's solution:
    Code:
    cin.ignore(1000, '\n');
    cin.get();
    The reason for std::numeric_limits<std::streamsize>::max() is to get the maximum size of the stream, and then discard as many characters as the stream can hold, or until the first '\n'. In this case, I have substituted for 1000, or whatever number you think is guaranteed to be less than what the user may type after the valid input.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Or you can just switch compilers.

    Dev-C++ MinGW GCC
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #25
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    conio is the problem

    Quote Originally Posted by ChronoSquare
    Would anyone here know why, when calling getch() from the conio library to pause the program until the user hits enter, that that line of code would not work?

    For some reason, in my program the getch has done just that - I can't think of any reason why it shouldn't work, and neither can my teacher. We are both baffled as to why the getch isn't functioning normally.

    I hesitate to post the code, because, I am not sure how much of the code would be required, and to put it all into a single post would be a bit much.

    Thanks to anyone for help!

    I hade the same problem.
    Instead "#include <conio>" use "#include <conio.h>.

  11. #26
    Registered User
    Join Date
    Jun 2006
    Posts
    2
    The conio is the problem. I hade the same problem. Your compilator cannot communicate correctly with conio. By somehow it still remember the mother C language of library
    Instead "#include <conio>" use "#include <conio.h>

  12. #27
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by seasali
    Your compilator cannot communicate correctly with conio.
    I've got to write that down somewhere once I stop laughing long enough.

    Quote Originally Posted by seasali
    Instead "#include <conio>" use "#include <conio.h>
    Nobody in this thread has used #include <conio> to my knowledge.

    Ok, back to business:
    Code:
    void average_scores(double scores[], int length)
    {  double sum, average;
    
    	scores[0] = 0;
    	scores[length] = 0;
       sum = 0;
    
       for(int m = 0; m < length; ++m)
       	sum = sum + scores[m];
    
       average = sum / (length - 1);
    
       cout << endl << "The average score given is: " <<
       	setiosflags(ios::showpoint|ios::fixed) << setprecision(2) << average << endl;
       getch();
    }
    What on earth do you expect that to do? Why do you clobber the first score, and then write to scores[length], which is out of bounds as people told you earlier?
    First, arrays in C (and C++) start at index 0. Hence scores[0] is the first item. If you declare an array as double scores[10], it has ten doubles: the first is scores[0], the last scores[9].
    Now, to rewrite your function:
    Code:
    void average_scores(double scores[], int length)
    {
    	double sum = 0, average;
    
    	for(int m = 0; m < length; ++m) sum += scores[m];
    	average = sum / length;
    
    	cout << endl << "The average score given is: " <<
       	setiosflags(ios::showpoint|ios::fixed) << setprecision(2) << average << endl;
       getch();
    }
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. Difference between getch() and getchar()
    By codec in forum C Programming
    Replies: 4
    Last Post: 04-04-2004, 01:34 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM

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