arrays and char.

Pages: 12
hello, i want my program to make user input 10 numbers into the array, but i also want the program to stop at any time when user inputs a letter instead of a number, i can't quite figure it out, can anyone please help me finish this?

And it keeps outputting: "enter a number:" several times after breaking the loop by entering a letter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

void getInput(int array[], int sizeOfArrray);
void displayArray(int array[], int sizeOfArray);


int main()
{

    int nArray[10] = {};
 
    
    getInput(nArray, 10);
    displayArray(nArray, 10);
}


void getInput(int array[], int sizeOfArray)
{
    
    
    for(int i = 0; i < sizeOfArray; i++)
    {
        
       cout << "Enter a number: ";
       cin >> array[i];
       if(array[i] == 's')
       {
           cout << "Hello world" << endl;
           
       }
     
    }
    
}


void displayArray(int array[], int sizeOfArray)
{
    cout << endl;
    for(int i = 0; i < sizeOfArray; i++)
    {
        cout << array[i] << endl;
    }
}
Last edited on
closed account (48T7M4Gy)
line 21 i < sizeOfArray
closed account (48T7M4Gy)
line 40 same fix required
ok done, but when i enter a letter it looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Enter a number: 1
Enter a number: 2
Enter a number: s
Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: 
1
2
0
0
0
0
0
0
0
0

i need to fix this somehow.
Last edited on
I just tried for like 15 minutes to make this work, and I see why you're having trouble :/

The problem you're running into is that when the user inputs a character instead of an integer, since you defined nArray as an integer and it therefore interprets the character as an integer.

You need to figure out a way to make another variable, lets call this one char charcheck;, and set it equal to the same value that the user inputs. In your if statement, you need to check if charcheck is between 'a' and 'z' or 'A' and 'Z'. If it is not, then the user did not input a character.

Alternatively, you can use the function isalpha to check if charchek is either an upper or lower case letter. isalpha uses both islower (checks for lower case) and isupper (checks for upper case) and if either of the two functions returns true, then isalpha returns true.

http://www.cplusplus.com/forum/beginner/136156/

^^^Thread that shows a simple example of how to use a similar isdigit function.^^^

So it would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
 
cin >> array[i], charcheck;

//You need these two to prevent getInput from looping all
//the way through (at least in my environment). I can explain if needed.
cin.clear();
cin.sync();

if ( isalpha(charcheck))
{
     cout << "Hello world!" << endl;
}


Since Hello World will now execute anytime the user inputs a character, you can write whatever code necessary to get the program to stop.

Anyone feel free to correct me if I'm giving any misinformation.
Last edited on
Ahh, the if statement with "hello world" is something i just forgot to remove when i was testing something. What i really wanted was the program to stop outputting "Enter a number:" when a specific letter was entered by user.
But i haven't worked with isalpha or isdigit yet and have no idea what they do, since i have only been learning C++ for a couple months (with no teacher). But i will take a look at it, thanks.
Last edited on
Well I already explained what isalpha does, but I'll do it again if you need.

Basically, isalpha() checks if the argument inside of it (the thing inside the parenthesis) is an alphabetic character, either upper or lower case. If the argument IS alphabetic, it will return true.

Since you need the argument inside of an "if" statement to be true for it to exectue, isalpha will provide you with that "true." In other words, if(true) will execute, and if(false) will not execute.

isdigit(1) = true
isdigit(A) = false

isdigit does the exact same thing, except it checks if the argument inside of it is any kind of number.

Okay, since you want the program to stop outputting "Enter a number" if the user enters a specific letter, it will look like this:

1
2
3
4
5
6
char lettercheck;

if ( lettercheck == 's' )
{
break;
}


"break" will break the looping of your for and will return back to your main function (that sounded super confusing).
Last edited on
Did you mean something like this?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void getInput(int array[], int sizeOfArray)
{
    
    for(int i = 0; i < sizeOfArray; i++)
    {
       char charcheck;
       cout << "Enter a number: ";
       cin >> array[i], charcheck;
       cin.clear();
       cin.sync();
       if(isalpha(charcheck))
       {
           break;
       }
     
    }
    
}

Last edited on
Yes, exactly. Except in your case, if you want the for loop to stop ONLY IF the user inputs a specific character, you would simply do if ( charcheck == 's' ). But as it is right now, it will end the loop if any alphabetical character is entered.
getInput function still output's "Enter a number: " several times after the break.

like so:
1
2
3
Enter a number: 1
Enter a number: a
Enter a number: Enter a number: Enter a number: Enter a number: Enter a number: 



This is what i have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49


void getInput(int array[], int sizeOfArrray);
void displayArray(int array[], int sizeOfArray);


int main()
{

    int nArray[7] = {};
 
    
    getInput(nArray, 7);
    displayArray(nArray, 7);
}


void getInput(int array[], int sizeOfArray)
{
    
    for(int i = 0; i < sizeOfArray; i++)
    {
       char charcheck;
       cout << "Enter a number: ";
       cin >> array[i], charcheck;
       cin.clear();
       cin.sync();
      
       if(isdigit (!charcheck))
       {
           break;
       }
     
    }
    
}


void displayArray(int array[], int sizeOfArray)
{
    cout << endl;
    for(int i = 0; i < sizeOfArray; i++)
    {
        cout << array[i] << endl;
    }
}


Last edited on
What environment are you using? I'm using CodeBlocks, and making sure the cin.clear and cin.sync are both in the program, it does not loop all the way through.

The break is supposed to stop the for statement... show me exactly what code you put in. It sounds like the if statement is not executing.
Last edited on
Oh, i'm using C++ Shell online compiler, and it doesn't break when a letter is entered in CodeBlocks i just discovered.

I enter something like this everytime just a few integers and then a letter to stop it.
1
2
3
4
5
6
7
8
9
10
11
12
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: f
Enter a number: Enter a number: Enter a number: 
1
2
3
0
0
0
0
Last edited on
Give me the entire code you are putting into the compiler. Never use an online compiler because they have always given me trouble.

In fact, copy and paste this entire thing into CodeBlocks and see if it still loops through:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

using namespace std;

void getInput(int array[], int sizeOfArrray);
void displayArray(int array[], int sizeOfArray);


int main()
{

    int nArray[10] = {};


    getInput(nArray, 10);
    displayArray(nArray, 10);
    return 0;
}


void getInput(int array[], int sizeOfArray)
{


    for(int i = 0; i <= sizeOfArray; i++)
    {
        char charcheck;

       cout << "Enter a number: ";
       cin >> array[i], charcheck;
       cin.clear();
       cin.sync();

       if( isalpha(charcheck) )
       {
           break;

       }

    }

}


void displayArray(int array[], int sizeOfArray)
{
    cout << endl;
    for(int i = 0; i; i++)
    {
        cout << array[i] << endl;
    }
}
It does but in another way, when i enter a letter it just keeps asking for "Enter a number:" just one at a time like i entered an integer instead of a lettter.
Last edited on
if(isdigit (!charcheck))

I don't know what you're doing here. You need to use isalpha, not isdigit. The "if" statement you have there will run if charcheck is a number, and that's not what you're telling me that you want. If you use isalpha, it will check if charcheck is a letter and then will proceed to break the loop.
Yeah i copied and pasted the code you wrote before with:
1
2
3
4
if( isalpha(charcheck) )
   {
           break;
   }


instead of the
if(isdigit (!charcheck))
, i just put it in to test something when it wasn't working.
I don't know why the if statement doesn't work :S.
Can it be because
cin >> array[i], charcheck;

cin >> integer, char in same statement?
Last edited on
Well honestly I don't know what else to tell you. If you copied the code exactly and and it still doesn't work, I don't know what the problem is. It works perfectly on my computer.
Can you copy what you enter and sent it to me? are you sure we are using the exact same code?
Did you copy everything I posted? Where I said "In fact, copy and paste this entire thing into CodeBlocks and see if it still loops through:", I posted the exact code you need to run.

If you copy and paste the ENTIRE thing so that ONLY that code is in your program, it should work. If it still doesn't work, I don't know what to do.
Pages: 12