#include <iostream.h>int main(){ const int x = 7; int count[x]; int num; for (int num=0; num<7; num++) { cout << "Enter exactly 7 numbers" << endl; cin>>count[num]; if(count[num]>x) cout<<"Too many numbers entered "<<endl; else cout << "\nThats it."<<endl; cout<<"\ninputs: " << count[num] <<"\n"; system("PAUSE"); return(0);}}
#include <string.h> // Don't forget the library that handles strings string number; //declares stringint i=0; //declares int for the loop cout << "Enter a number : ";getline(cin, number); //Inputs number while(i <= number.length()) //Loop that goes through all the letters{ if(number < '0' || number > '9') //Does the character have a ASCII value under 0 or above 9? { cout << "\nThat isn't a number!!"; return 0; } i++; //If it passed the check above, then it's time for the next letter}
#include <iostream.h>int main(){Â Â int count[7]; //elements 0 - 6Â Â int x = 0;Â Â int temp;Â do { Â Â Â Â Â Â Â Â Â Â Â cout << "Enter number" << endl;Â Â Â Â Â Â Â Â Â Â Â cin >> temp; if(x>6) break;Â Â Â Â Â Â Â Â Â Â Â if(temp>7) {Â Â Â Â Â Â Â Â Â Â Â cout << "Too high." <<endl; continue; } Â Â Â else if(temp < 0) { cout << "too low" << endl; continue; } else { count[x] = temp; x++; } Â Â Â Â Â Â Â Â Â Â }while(true); for(int i=0;i<x;i++) { cout << " " << count[i]; }system("PAUSE");return 0;Â Â Â Â Â Â Â Â Â Â }
thanks reaper and krenz.. that actually helped a lot.as for this q2 noobie flamethread starter k_ ...if you have nothing useful to say, don't respond to my threads, sojust move along, cuz from here on out you will just be ignored.had I just asked someone to do my hw, I would have posted a problem and asked for a solution, not providedany code.thanks again to the old q2'ers willing to help the infamous [N]auTiCa
just saying you won't learn from asking other people for answers.
Hey hows everyone been..?Haven't been around the Q2 community for awhile and what better wayto come back then to ask for help Anyway, I'm having a bit of trouble with the following code.You are to input 7 numbers then print, however if you input anything other than a numberhigher than 7 it says it's to high. I need user input to type in any 7 numbers from 0-9then print all 7 on one line. If they input more than 7, it's to give an error then exit.Heres what I have thus far.Code: [Select]#include <iostream.h>int main(){ const int x = 7; int count[x]; int num; for (int num=0; num<7; num++) { cout << "Enter exactly 7 numbers" << endl; cin>>count[num]; if(count[num]>x) cout<<"Too many numbers entered "<<endl; else cout << "\nThats it."<<endl; cout<<"\ninputs: " << count[num] <<"\n"; system("PAUSE"); return(0);}}Any help would be greatly appreciated.p.s. hey console.. ltns
Reaper also calls istream::getline without proper argument specifying the size of the buffer, leading to a potiential buffer overflow
#include <iostream>#include <cstdlib>#include <string>using namespace std;// this function receives user input as a string and checks// for valid digits. If all valid decimal digits, return the string,// else return an empty string.string getnumber(void){ string number; //declares string int i; //declares int for the loop cin>>number; // input a string i = 0; // initialize loop variables close to top of the loop while(i < number.length()) //go through all the letters except the termination character { if(!isdigit(number[i])) // are all valid digits 0 thru 9? return "\0"; // invalid digit found, return empty string i++; } return number; // return a valid number string}// call the getnumber function 7 times and print the resultsint main(int argc, char *argv[]){ int i; const int maxnum = 7; int count; //count the valid numbers returned string number[maxnum]; count = 0; for(i = 0; i < maxnum; i++) { cout << "Enter a number : "; number[i] = getnumber(); if(number[i] == "\0") { cout << "That isn't a decimal number!!\n"; break; } count++; } cout<<"\n"<<i<<" valid numbers received:\n"; for (i = 0; i < count; i++) cout<<number[i]; // print the numbers cout<<"\n"; return 0;}
numbers = []while numbers.length < 7 print "Enter a number: " input = gets.chop num = Integer(input) rescue nil if num.nil? puts "YOU FAIL!" elsif ! num.between?(0, 9) puts "Close but no prize." else numbers << num endendputs numbers.join(" ")
int main (int argc, char * const argv[]) { string number; cout << "Enter exactly 7 digits" << endl; cin >> number; if (number.length() > 7) cout << "Too many digits!" << endl; else cout << number << endl;}
As for quadz.. wtf is ruby? lol