Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

need some help with this code c++

+1 vote
asked Nov 30, 2022 by Alan Corleto (360 points)
retagged Nov 30, 2022 by Alan Corleto
Hi everyone I need tome tips because I'd to learn how to create a validation process, I want the program to only take int variables, and if the user write something else, there is a pront that tell to try again but whenever I try to to it it's just go automatically to infinite loop, let me get you an example

int grades;

cout << "Write the grades" << endl;  

// if the user write a letter or something else I want to promt that need it to write something again, i wish I can do it with funtion isalpha() but I try it and I guess I just don't know how to use it.

// the way a try it

cin >> grades;

while(grades != 0)


cout << "Try again" << endl;

cin >> grades;


Something else I'd like to know if you don't mind if that I'm trying to repeat a code as well but when I try to do it, it's skipping the first part and I don't know why, still running though but just that, this is an example

#include <iostream>

#include <string>

using namespace std;

int main()


char repeat;

do {

int age;

string fullname;

cout << "enter your full name" << endl;

getline(cin, fullname);

cout << "enter your age" << endl;

cin >> age;

cout << "\n \n \n";

cout << fullname << endl;

cout << age;

cout << "\n \n";

cout << "press (y) to repeat" << endl;

cin >> repeat;

}while(repeat == 'y');

 return 0;


/* i know is a little messy but it is just an example, I've been working with it for a few hours but I can't figure it out I guess    THANK YOU IN ADVANCE!!*/

3 Answers

0 votes
answered Dec 1, 2022 by David Abel illanis huaraca (140 points)
/*plis lenguje in Spanish*/

using namespace std;

int main(){
    int longitud, i, int_n;
    string b="";
    char num[100];
        cout<<"ingrese su numero: "; cin.getline(num,90,'\n');
        for(i=0; i<longitud; i++){
            if(num[i]=='0'|| num[i]=='1'|| num[i]=='2'|| num[i]=='3'|| num[i]=='4'|| num[i]=='5'|| num[i]=='6'|| num[i]=='7'|| num[i]=='8'|| num[i]=='9'){
                cout<<"ingrese solo numeros\n\n";
                goto cifras;
    cout<<"Tu numero "<<int_n<<"+4="<<int_n+4;

    return 0;
0 votes
answered Dec 1, 2022 by Petar Bogdanov (140 points)
https://pastebin.com/KETuVBqi I hope this code will help you understand. I think I predicted every outcome, but if not feel free to fix it and let me know. If you have questions, you can ask me.
0 votes
answered Dec 2, 2022 by dezDawg221 (140 points)

1.) to answer your first question about the infinite loop. This is occurring because did not initialize the variable grades on line 1. It is good common practice to initialize new variables you make so the system knows about the variable. So if you change your first line of code to something like:

int grades = 0;

it should solve the issue.

2.) As for the 2nd part, I think if you initialize your char repeat to some value it might solve your problem.

char repeat = '  ';

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.