C/C++ :: If Statement Comparing Array To Scanf Input
Oct 25, 2014
The issue that I'm having is that when I run this part of the code it seems to enter the if statement and set it to 0 when comparing the array to the input.
What I want it to do is to search the array for the grade, set it to zero, and then exit the loop in case there are any repeat grades. It always sets the first number to 0 and then exits.
//Below is the prototyping for the array
int grades[14] = {60,50,50,20,75,90,0,0,0,0,0,0,0,0};
//There is some code between here, but it is probably not relevant so I didn't post it.
printf("
Enter the grade that you want to remove. Press Enter after each input.
");
printf("Enter 0 to exit:
");
[Code] ....
I didn't mean to leave the "prototyping" part in the code. I was initially going to put the function prototypes in there, but decided it wasn't necessary for the question.
View 6 Replies
ADVERTISEMENT
Aug 7, 2013
When I do this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main () {
int guess ;
int number = 5 ;
[Code] .....
But when I do this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main () {
int guess ;
int number = 5 ;
[Code] ....
Why does the compiler say that there is an else without a previous if ??? ... Does this mean that I cannot use "scanf" in an if/else statement?
View 2 Replies
View Related
Feb 25, 2013
I want to compare the part of the character array with the scanned input. I've initialized the character array (colourCompare).
What I want to do is, if the input colour matches up with one of the elements in the colourCompare array, it will then read the next value(I did not include "read the next value part"). If the input does not match up, then it goes back to the scanning part.
Code:
char colourCompare [12][6] = {"Black","Brown","Red","Orange","Yellow","Green","Blue","Violet","Gray","White","Gold","Silver"};
float resistanceCal() {
[Code]....
View 14 Replies
View Related
Jul 1, 2014
I am trying to do this but it can never seem to work.
#include <iostream>
using namespace std;
int main () {
char charOne;
cin.get(charOne);
cin.ignore(1000, '
[Code] .....
Basically i want to keep looping until i enter an a or A (also does this apply to if statements as well?)
View 6 Replies
View Related
Aug 31, 2014
i actually i want store string in r and try to compare other string (room_no) in function check but i try many time it still having error
#include<iostream>
#include<conio.h>
#include<fstream>
[Code].....
View 2 Replies
View Related
Aug 13, 2014
I would just like to know what does
while( (c =getchar())!='
' && c !=EOF );
do ? and how it do it?
I have see it in a example to clear the input buffer for next input through scanf but does not know its working clearly.
it is used in this example :
Code:
#include<stdio.h>
struct linklist {
char value[20];
struct linklist *next;
};
struct linklist *head,*curr;
[Code] .....
View 4 Replies
View Related
Mar 31, 2014
In GCC v4.6, I am encountering a strange problem. If I try to get a character input and a int input like this:
Code:
int main()
{
int a;
char b;
scanf("%d",&a);
scanf("%c",&b);
}
the compiled program asks for input only one time. On the other hand, if I do the char scanf earlier like this:
Code:
int main()
{
int a;
char b;
scanf("%c",&b);
scanf("%d",&a);
}
it asks for input twice.
View 6 Replies
View Related
Oct 28, 2014
How to store the value in this case.. The topic is called "limited summation".. The following is the guideline for this problem:
Write a program in a folder called "sum" with a source file of main.cpp that does the following:
•prompt a user to enter a maximum number of entries, make the prompt "max # entries"
•prompt a user to enter a threshold sum, make the prompt "threshold"
•using error checking logic, let a user enter base-10 signed numbers until at least one of the following conditions is true:
or the maximum number of entries is reached
or the sum of entered numbers has reached (>=) the threshold
•print the sum of all the entries, just the number and a linefeed at the end of the line
Error checking means entries that are not numbers are detected and ignored. You are to use printf and scanf in this assignment (no cin or cout).
View 3 Replies
View Related
Apr 19, 2013
I tried to scanf the input by the user into the function but it does not read my input. Read on one of the thread, it said that to scanf a input into a double, need to use %1f instead to %d (which normally used. I tried changing it to %1f and it still did not work.
Code:
#include <stdio.h>
#include <math.h>
/* function main begins program execution */
int main( void )
{
double amount; /* amount on deposit */
[Code] ....
View 2 Replies
View Related
Oct 30, 2013
so i'm using scanf() this way
Code:
int i;
while (scanf("%i", &i))
printf("%i ", i);
printf("
done
");
i tried several combination of Ctrl+D and "Enter", it's not terminating, Ctrl+C just "cancels" the whole thing without printing "done", i'm running this on linux installed on a PC
View 9 Replies
View Related
Nov 1, 2013
So running the following code
Code:
#include <stdio.h>
int main(void) {
char c;
int i=1;
while (scanf("%c", &c)==1)
printf("loop sequence %i: %c(%i)
[Code] ......
Done it seems a "carriage return" serves two purposes here, one is to signal the program to read in the character typed in before the "carriage return", another serves as a second character typed, how can i do this cleanly, that is without having to use a "carriage return" as the second character to signal "I've typed in the first character already".
View 2 Replies
View Related
May 2, 2013
Code:
#include<stdio.h>
#include<windows.h>
int nr, i, array_new[10],n=0;
main(){
printf ("Insert number: ");
scanf ("%d", &nr);
[Code]...
I need put value from scanf into array. How make this: array_new[nr]?
View 8 Replies
View Related
Nov 23, 2014
I don't know how many numbers will be the input only that its going to be up to 10000. EOF should be active. I tried it like this:
Code:
int i = 0;
int del[10000]
while (scanf("%d",del[i])!=EOF)
{
i++;
}
But it seems the value of i doesn't increment, could you provide some tips on how to scanf the inputs into an array if I don't know how many numbers will I have?
View 7 Replies
View Related
Oct 5, 2014
I'm trying to make a character array, and be able to custom fill it by using the scanf() function to ask the user for each character spot in the array.
Code:
#include <stdio.h>
int main() {
int i;
char character_array[11];
char *charpointer;
charpointer = character_array;
[Code] .....
And this is the output i get when i run it:
Code:
root@kali-Tulips:~# ./myown
what letter would you like in the [0] spot of the array? :
a
what letter would you like in the [1] spot of the array? :
what letter would you like in the [2] spot of the array? :
b
what letter would you like in the [3] spot of the array? :
what letter would you like in the [4] spot of the array? :
[Code] .....
Obviously I'm trying to grasp C programming and am very new. I don't understand why this isn't working, and i think its more than my lack of knowledge of C syntax. I believe the correct memory is allocated but when examined with gdb i don't find what i expect....
View 4 Replies
View Related
Mar 6, 2015
how to compare each element of array with another,defined ? I have tried this,but didn't work.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bill()
[Code].....
View 3 Replies
View Related
Feb 6, 2013
i have 3 arrays total, 2 of them i am comparing to see if any of the values match at all, and then i am putting the value that matches into the 3rd array. currently i am trying
int isthere (int match[], int maxmatchLength,
const int vec1[], int vec1Length,
const int vec2[], int vec2Length)
{
for (i=0; i<vec1Length; i++)
if vec1[i]==vec2[i];
vec1[i] = match[i];
}
But this will just match the same values are in the same spot. how do i fix it so that it compares one value in the first array to the whole second array, before going to the next number.
View 1 Replies
View Related
Jun 22, 2014
I am working on a program to find uppercase, lowercase and digits in a 2D char array. When I try to use an if statement to increase the counter, I get an error "no conversion from 'int' to 'char*'". This is the if statement I am using.
if(myArray[j] <='9' || myArray[j] >='0')
View 7 Replies
View Related
Jul 13, 2013
I have function that looks like this myfoo(char* Name) Now i want to compare this name to another one . But the another name is a pointer . This my code :
bool Tribe::RemoveSurvavior(char *H_Name) {
const char *p;
p=SurpointArr[i]->GetSurvivor_Name();
}
I need to compare if p is same as H_Name.
Mine is do it with for on each element but when i use sizeof it gives me size of char and not real size of the name.
View 6 Replies
View Related
May 29, 2014
I wrote a program that reads a list from a file and stores it in a string type vector. Now, I want the user to input a word so that the program can search the vector to see if that word already exists. I have used every possible way of reading input from the console and storing it in order to compare with the vector but it never results in a match. When I print the input string and the vector string they are exactly the same thing (or at least print to the console as if they were). I've tried using getline; using cin direct to a string var; using cin to a char array and then casting to string using string str(arr); I even added a newline at the end just in case and STILL I cannot get a match.
vector <string> currentSet; //read a list in from a file and has 9 items in it
cin.ignore();
string line;
getline(cin, line);
if(line == vector[0]){//if printed to console line is HEAT and vector[0] is HEAT
cout<<"match"<<endl;
}
View 3 Replies
View Related
May 3, 2013
I'm working on a CGI application. I'm trying to test my input with a switch statement and output the result with html tags to populate a web page. From within the switch, I've coded as follows:
HTML Code:
switch(mFunc) {
case 0:
cout << "<p><b>YOU ENTERED THE FOLLOWING TO BE CALCULATED:</b></p>" "<h2>"<< number1 <<"+" << number2 << "</h2>" << endl;
break;
case 1:
cout << "You've entered" << number1 <<"-" << number2 << "to be evaluated" << endl;
break;
I know that I'll need to put this in an html body with a content type as such:
HTML Code:
cout << "Content-type: text/html
";
cout << "<html><body>
";
Am I able to do that directly inside of the switch statement?
View 11 Replies
View Related
Feb 7, 2013
I am stuck in this program, Be given a string of chars, where each single char belongs to the following alphabet: a..zA..Z0..9 (So, in the string there are only lowercases, uppercases and digits. No blank, no comma, ...). For every char of the given alphabet, count how many times in the string
1-- the char belong to a sequence of identical chars whose length is at least three (i.e.: in the string cc74uyrpfccc348fhsjcccc3848djccccc484jd for three times the character 'c' satisfies this condition)
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
[Code]...
2-what is the longest substring of characters strictly rising interm of ASCII code(the following is greater (>) of the previous)
3- what is the longest substring of successive characters interm of given string rannge (i.e.: fhkjshdfruytyzABCDEfglsj => 7)
View 1 Replies
View Related
Nov 17, 2013
how would i got about making an if statement for an array so it prints what u enter
Code:
example:
struct input{
float peopleseen;
float creditrate;
char insurprov[12];
[Code] ....
View 1 Replies
View Related
Apr 15, 2014
so I am trying to have an switch statement array. but every time i complete a case statement the whole process is returned. the thing is i want the menu to stay up even if i complete the case.
View 2 Replies
View Related
Mar 16, 2014
#include <iostream>
#include <iomanip>
using namespace std;
double calculateCharges(double hours[], double sizeofArray);//call function
[Code]....
View 3 Replies
View Related
Mar 25, 2014
Just making typical console game where hero moves with w a s d.
However, in my code, while a and w work (minusing), s and d dont work (adding). I have used a switch statement to change the char position of the hero based on kbhit.When you hit s or d, the buttons that add 1 to the 2nd dimension of the array, the screen goes crazy!
#include <iostream>
#include <string>
#include <conio.h>
#include <ctime>
#include <cstdlib>
using namespace std;
const char PLAYER = 'H';
[Code] .....
View 3 Replies
View Related
Dec 23, 2014
I've made a code to check whether or not a save file has been created correctly, but for some reason it always returns this line: readdata[qa]=='1' as true. in which qa is the counter I use in a for loop and readdata is a character array consisting of 50 characters that are either 0, 1 or 2.
this is the entire code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
[Code]....
at first is also went wrong at line 22 and also returned that as true, but then I added brackets and it worked.
View 4 Replies
View Related