How to stop duplication and make letter input work instead of numbers from keyboard

closed account (L10oLyTq)
Having trouble with code here and im a beginner could someone tell me how to make this part of my code stop duplication

Put the code you need help with here.
[/code]
Void create() //creating an array
{
cout<<("\nEnter the size of the array elements:\t");
scanf("%d",&n);
cout<<("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}



and how to make this part accept lower and uppercase letters Thanks a million :)




#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include <string.h>
using namespace std;

int a[20],b[20],c[40];
int n,val,i,j,key,pos;

void create();
void Print();
void insert();
void del();
void search();
void clear();
int main()
{
int choice=toupper(choice);

do{
cout<<("\n\n--------Menu-----------\n");
cout<<("1.(A)dd\n");
cout<<("2.(P)rint\n");
cout<<("3.(I)nsert\n");
cout<<("4.(D)elete\n");
cout<<("5.(F)ind\n");
cout<<("6.(C)lear\n");
cout<<("7.(E)xit\n");
cout<<("-----------------------");
cout<<("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2:
Print();
break;
case 3:
insert();
break;

case 4:
del();
break;
case 5:
search();
break;
case 6: clear();
break;



case 7:
cout<<("\n Thank-you for using the program:\n");
exit(0);
break;

default:
cout<<("\nInvalid choice:\n");
break;
}
}
while(choice!=8);
Hi,

Having trouble with code here and im a beginner could someone tell me how to make this part of my code stop duplication


There are some standard libraries in c++ which do that stuff

http://www.cplusplus.com/reference/algorithm/unique/


and how to make this part accept lower and uppercase letters

what?
I dont get this one?
You are taking integer for choice and still want to convert it to upper case and lower case?
Don't mix C and C++ I/O.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int num_elements;
cout << "How many elements? ";
cin >> num_elements;

int* elements = new int[ num_elements ];

cout << "Enter " << num_elements << " integers; no duplicates allowed:\n";
for (int n = 0; n < num_elements; )
{
  cout << "element " << n << "? ";
  cin >> elements[ n ];
  if (std::count( elements, elements+n, elements[ n ] ) > 1)
  {
    cout << "(removed duplicate: " << elements[ n ] << ")\n";
  }
  else n++;
}

Hope this helps.
closed account (L10oLyTq)
I'm still getting an error cout isnt not a member of std have included my iosstream and using namespace std;

could you tell me why this happens??
iosstream is iostream
closed account (L10oLyTq)
ok so i have changed my create void to the following and im getting these errors

void create() //creating an array

{

if(n > 0){

for(var i; i > n; i++){

if(a[i] == cin){
cout<<("\nThe entered value is already contained in the collection\n");
}else{
scanf("%d",&a[n++]);

}

}

}else{

scanf("%d",&a[0]);

}
cout<<("\nEnter the size of the array elements:\t");
scanf("%d",&n);
cout<<("\nEnter the elements for the array:\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
}//end of create()


||=== Build: Debug in practice (compiler: GNU GCC Compiler) ===|
C:\Users\csf14\Pictures\Programming\practice\main.cpp||In function 'void create()':|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|76|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|76|warning: right operand of comma operator has no effect [-Wunused-value]|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|error: no match for 'operator==' in 'a[i] == std::cin'|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: candidates are:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\postypes.h|218|note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\postypes.h|218|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::fpos<_StateT>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_pair.h|212|note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_pair.h|212|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::pair<_T1, _T2>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_iterator.h|293|note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_iterator.h|293|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::reverse_iterator<_Iterator>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_iterator.h|343|note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_iterator.h|343|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::reverse_iterator<_IteratorL>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\allocator.h|119|note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\allocator.h|119|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::allocator<_T1>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\allocator.h|124|note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\allocator.h|124|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::allocator<_Tp1>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2483|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2483|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::basic_string<_CharT, _Traits, _Alloc>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2490|note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2490|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::basic_string<_CharT>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2504|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2504|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const _CharT*' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2516|note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2516|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::basic_string<_CharT, _Traits, _Alloc>' and 'int'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\streambuf_iterator.h|206|note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\streambuf_iterator.h|206|note: template argument deduction/substitution failed:|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|78|note: mismatched types 'const std::istreambuf_iterator<_CharT, _Traits>' and 'int'|
C:\Users\csf14\Pictures\Programming\practice\main.cpp||In function 'void search()':|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|131|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\Users\csf14\Pictures\Programming\practice\main.cpp||In function 'void Delete()':|
C:\Users\csf14\Pictures\Programming\practice\main.cpp|162|warning: left operand of comma operator has no effect [-Wunused-value]|
||=== Build failed: 1 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|
var i; is great for javascript, but it doesn't works with c++, its int i; and also you have to initialize it to zero (only in this case)...

Also i > n; that's wrong... right?

And if(a[i] == cin) is also wrong
Last edited on
Topic archived. No new replies allowed.