Ensuring a number input/lowercase check/writing txt problem

Hello!

I am having a couple of problems with an assignment I've been given. Coule you please give me a hand so that I can finish it?

1) [SOLVED] I want to confirm when a user inputs a number, for it not to be a letter.

1
2
3
4
5
6
7
8

int amount
cout<<"Input amount"<<endl;
cin>>amount
/*and here's where I want to check that it's a number and not a letter, displaying "Please make sure to type a value between 1 and 200." or something alike.
What I thought is 
"if amount not in (1..200) {}
else (...)*/


How could I?


2)[SOLVED, BUT HAS A MINOR ISSUE] I'm trying to get my program to write a text file when certain conditions are met, but the compiler (IDE?) keeps telling me there's a typo (|43|error: expected primary-expression before ';' token|) on the lines that start with "case".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void escrituraResultados (int ganador, string player1, string player2,int turnosRemanentes,int turnosDisponibles, string palabraaAdivinar)
{
  fstream resultados ("Resultados.txt");
  if (resultados.is_open())

  {
      /*switch (ganador)
        {
        case 1: resultados << player1<<" ganó. La palabra secreta era "
<<palabraaAdivinar<<"."<<endl<<;
                resultados << "Se le dio a "<<player2<<" "<<turnosDisponibles<<endl;
                resultados.close();
                break;

        case 2: resultados << player2<<" adivinó la palabra secreta ("<<palabraaAdivinar<<")."<<endl<<;
                resultados << "Se le dieron "<<turnosDisponibles<<" turnos, y le sobraron "<<turnosRemanentes<<endl;
                resultados.close();
                break;
        }*/
  }
  else cout << "Can't open file.";
}


What's wrong there? I can't see the error...


3)[SOLVED] Lastly, I want to know how could I make sure that a string the user inputs is stored in memory, is stored entirely in lowercase.
(User inputs <<HoUSe>>, program interprets <<house>>)
I know of the tolower function, but I don't know if it's right... or how to use it.


Any help'd be greatly appreciated!
Last edited on
3)
1
2
3
4
5
#include <algorithm>
#include <string> 

std::string myString = "HoUSe"; 
std::transform(myString.begin(), myString.end(), myString.begin(), ::tolower);


2)
 
<<endl<<;

Remove the 2nd set of "<<".

The compiler is the program that 'transforms' your code into stuff that your computer understands. An IDE is just the software that allows you to type stuff in really. For example these are IDEs:
Visual Studio
Code::Blocks
Notepad

Any text editor really.
Last edited on
Since #2 and #3 has been answered, I'll answer #1.

1)
If cin fails to acquire input (i.e. when the user inputs letters when an integer was expected), you can check for it using cin.fail(). Put it in an if statement. Then give an error message and clear and reset the stream using cin.clear() and cin.reset().

Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    int num;
    cout << "Enter a number: ";
    cin >> num;

    if(cin.fail()) //This means cin failed to acquire input
    {
        cout << "Thats not a number!" << endl;
        cin.clear(); //Clear the stream
        cin.ignore(); //Ignore what ever is there
        cout << "Please enter a number: ";
        cin >> num;
    }

    cout << "You entered number: " << num << endl;
    return 0;
}
Thanks!! I had never heard of "transform". That'll come in handy.

As for 2), I don't know how I missed that, but, well... I did.

EDIT: I hadn't seen your reply, stormboy.

It does work, and placing it in a while cycle fixed my problem. Thank you :)
Last edited on
One more (hopefully last) question regarding point 2).

I have changed

fstream resultados ("Resultados.txt");
if (resultados.is_open())
{code}

to

fstream resultados ("Resultados.txt");
if (resultados.is_open(), fstream::app)


And while it does compile, it deletes and re-writes the file from zero each time the function is called.

What's wrong? How could I get the file to open and keep writing, either above or below what's already in it?
 
if (resultados.is_open(), fstream::app)

Using a comma operator in an if statement makes no sense.
If you want to specify the append option, you must do so when opening the file.
 
resultados.open("Resultados.txt", fstream::app);



Topic archived. No new replies allowed.