[RESOLVED] Problem implementing ToUpper
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] Problem implementing ToUpper

  1. #1
    Join Date
    Nov 2013
    Posts
    10

    [RESOLVED] Problem implementing ToUpper

    I have searched and tried so many combinations to implement ToUpper into my program but it simply doesn't work so I'm resorting to the message boards!

    I'm coding in C# and I use Visual 6 C++ at Uni and Codeblocks at home and it works on neither.

    Currently I'm using (repeatTest=='Y' || repeatTest=='y') but I want to implement <ctype.h> and ToUpper so that even if a lowercase y is entered it still accepts it.

    I've tried

    char repeatTest = repeatTest.ToUpper();
    repeatTest = repeatTest.ToUpper();

    both say 'ToUpper' in something not a structure or union

    and many other combinations.

    Here's my working code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
    	char repeatTest='Y';
    
    	while (repeatTest=='Y' || repeatTest=='y')
            {
    		char answer=0, correctAnswer=0, questionNumber=1, testResult=0, NumberOfQuestions=1;
    		srand (time(0));
    
                    printf ("Test will begin, good luck\n\n\n\n");
    
                    for (NumberOfQuestions=0; NumberOfQuestions!=10; NumberOfQuestions++) {
                            int randomValue1=rand()%12+1;
                            int randomValue2=rand()%12+1;
                            printf("%i. What is %i x %i?    ",questionNumber, randomValue1, randomValue2);
                            scanf("%i", &answer);
    
                            if (answer==(randomValue1*randomValue2)){
                            printf ("Correct\n\n");
                            ++correctAnswer;
                            ++questionNumber;
    			}
    
    			else {
    				printf ("Incorrect\n\n");
    				++questionNumber;
    			}
    		}
    
                   testResult = correctAnswer * 10;
                   printf ("You answered %i%% of the questions correctly\n\n",testResult);
                  "\n\n";
                   printf ("Would you like to have another go? Y or N\n\n");
                   scanf (" %c", &repeatTest);
    
                    if (repeatTest=='N' || repeatTest=='n'){
                            return 0;
                    }
    
                   system("cls");
    
    	}
        system("pause");
    }
    Any responses would be appreacted

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem implementing ToUpper

    Quote Originally Posted by cassas00 View Post
    I have searched and tried so many combinations to implement ToUpper into my program but it simply doesn't work
    http://www.cplusplus.com/reference/cctype/toupper/
    I'm coding in C#
    Some advice -- don't use C# as a guide in attempting to write C++ code.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2013
    Posts
    10

    Re: Problem implementing ToUpper

    Ah, I think I'm getting too far ahead of myself. I'll wait to see how my tutor implements it in c# in my upcoming lessons rather than using c++ code lol thanks!

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,880

    Re: [RESOLVED] Problem implementing ToUpper

    Code:
    while (repeatTest=='Y' || repeatTest=='y')
    becomes
    Code:
    while(toupper(repeatTest) == 'Y')
    note lowercase.

    you could also have
    Code:
    repeatTest = toupper(repeatTest);
    after the scanf.

    You also need
    Code:
    #include <ctype.h>
    at the beginning of the program.

    Paul offered some good advice - although in certain respects c# bears some resemblance to c++, don't use it as a guide as to how to write c++ programs.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)